Rails form_for提交按钮不起作用 [英] Rails form_for submit button not working

查看:161
本文介绍了Rails form_for提交按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的耐心。对于Rails来说,它仍然很新颖。



使用Rails 3.2



制作一个简单应用程序的注册页面。我的问题是我的表单上的提交按钮不会产生任何影响,无论表单中的信息是否有效。

用户模型和数据库似乎都很好。如果我从rails控制台手动添加用户,它会将其添加到数据库。据我所知,这个问题似乎是由form_for产生的形式。



这是有问题的页面:

 <%provide(:title,'注册')%> 
< h1>注册< / h1>

< div class =row>
< div class =span6 offset3>
<%= form_for(@user)do | f |%>
<%= render'shared / error_messages'%>

<%= f.label:name%>
<%= f.text_field:name%>

<%= f。标签:电子邮件%>
<%= f.text_field:电子邮件%>

<%= f.label:密码%>
<%= f.password_field:密码%>

<%= f.label:password_confirmation,确认%>
<%= f.password_field:password_confirmation%>

<%= f.submitCreate my account,class:btn btn-large btn-primary%>
<%end%>
< / div>
< / div>

这里是我的用户控制器:

  def show 
@user = User.find(params [:id])
end

def new
@user = User.new
结束
$ b $创建
@user = User.new(params [:user])
if @ user.save
flash [:success] =谢谢注册!
redirect_to @user
else
渲染'新'
结束
结束
结束

提交不会导致任何类型的错误 - 根本不会导致任何事情发生。



编辑:从/ shared / _error_messages中添加代码.html.erb partial

 <%if user.errors.any?%> 
< div id =error_explanation>
< div class =alert alert-error>
表单包含<%= pluralize(@ user.errors.count,error)%> ;。
< / div>
< ul>
<%@ user.errors.full_messages.each do | msg |%>
< li> < / b>
<%end>
< %>


解决方案

那么这里有一个精心编写的小课程:

 < div class =row > 
< div class =span6 offset3>
<%= form_for(@user)do | f |%>

应该是:

 < div class =row> 
< div class =span6 offset3 ****>
<%= form_for(@user)do | f |%>



结束标记可以产生很大的差异。


Thank you for your patience. Still pretty new to Rails.

Using Rails 3.2

Making a signup page for a simple app. My problem is that the submit button on my form doesn't cause any effect, whether the information in the form is valid or not.

The User model and database both seem to work fine. If I add a user manually from the rails console, it will add it to the database. As far as I can tell, the issue seems to be in the form generated by form_for.

Here is the page in question:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
    <div class="span6 offset3>
        <%= form_for(@user) do |f| %>
            <%= render 'shared/error_messages' %>

            <%= f.label :name %>
            <%= f.text_field :name %>

            <%= f.label :email %>
            <%= f.text_field :email %>

            <%= f.label :password %>
            <%= f.password_field :password %>

            <%= f.label :password_confirmation, "Confirmation" %>
            <%= f.password_field :password_confirmation %>

            <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
        <% end %>
    </div>
</div>

And here is my users controller:

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
        if @user.save
            flash[:success] = "Thank you for signing up! Please check your email to confirm your account."
            redirect_to @user
        else
            render 'new'
        end
    end
end

Submit doesn't cause any type of error - simply doesn't cause anything to happen at all. So no valuable information in the logs.

Thank you in advance for any insight.

EDIT: Adding code from /shared/_error_messages.html.erb partial

<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li>* <%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

解决方案

Well there's a nice little lesson in careful coding:

<div class="row">
    <div class="span6 offset3>
        <%= form_for(@user) do |f| %>

should have been:

<div class="row">
    <div class="span6 offset3**"**>
        <%= form_for(@user) do |f| %>

Quite a difference a closing tag can make.

这篇关于Rails form_for提交按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆