1

我正在使用bootstrap-sass gem 在 Rails 3.2.13 中创建一个小应用程序。我有以下情况:

我的 'sign_up' 控制器有和 'index' 操作来创建这样的用户:

#sign_up controller
def index
    @user = User.new
end

现在,在我的 index.html.erb 中,我有以下形式:

# sign_up controller => index.html.erb
<%= form_for @user, :html => {:class => 'form-horizontal'} do |f| %>    
<div class = 'control-group'>
    <%= f.label :username, :html => {:class => 'control-label'} %>      
    <%= text_field_tag :username, nil, :placeholder => 'Username' %>
</div>
<div class = 'control-group'>
    <%= f.label :email, :html => {:class => 'control-label'} %>     
    <%= email_field_tag :email, nil, :placeholder => 'Email' %>
</div>
<div class = 'control-group'>
    <%= f.label :password, :html => {:class => 'control-label'} %>
    <%= password_field_tag :password, nil, :placeholder => 'Password' %>
</div>
<div class = 'control-group'>
    <%= f.label :confirm_password, :html => {:class => 'control-label'} %>
    <%= password_field_tag :confirm_password, nil, :placeholder => 'Confirm' %>
</div>
<%= f.submit 'Create', :class => 'btn btn-primary'%>

这将 POST 到 users_controller,调用 create 操作:

 # users controller
 def create
  @user = User.new(params[:user])

   respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render json: @user, status: :created, location: @user }
    else
      format.html { render action: "new" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

问题是:@user 被保存了,但是参数丢失了!在控制台日志中:

Started POST "/users" for 127.0.0.1 at 2013-05-29 12:14:47 -0300

由 UsersController#create 作为 HTML 参数处理:{"utf8"=>"✓", "authenticity_token"=>"wI5sNQV/lkn8GtYv5YkRLZ6HzxPg5MtoVXuarLulv30=", "username"=>"nonickname", "email"=>"no@bar. com", "password"=>"[FILTERED]", "confirm_password"=>"[FILTERED]", "commit"=>"Create"} (0.1ms) 开始事务 SQL (0.9ms) INSERT INTO "users" (“admin”,“created_at”,“email”,“password_hash”,“password_salt”,“updated_at”,“username”)值(?,?,?,?,?,?,?)[[“管理员” , nil], ["created_at", 2013 年 5 月 29 日星期三 15:14:47 UTC +00:00], ["email", nil], ["password_hash", nil], ["password_salt", nil], ["updated_at", Wed, 2013 年 5 月 29 日 15:14:47 UTC +00:00], ["username", nil]] (148.9ms) 提交事务...

为什么会发生这种情况,我该如何解决?我知道表单应该是 users_controller 域(我打算修复它),但这似乎不是问题!

谢谢你的帮助!

4

2 回答 2

1

我会尽我所能在这里帮助你。在我创建评论时查看控制台:

Started POST "/companies/1/comments" for 127.0.0.1 at 2013-05-29 20:06:02 +0300
Processing by CommentsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+J+ewToARuBybidK79V/3cFdX6=", "comment"=>{"content"=>"Lorem ipsum..."}, "commit"=>"Add Comments", "company_id"=>"1"}
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `comments` (`commentable_id`, `commentable_type`, `content`, `created_at`, `updated_at`, `user_id`) VALUES (1, 'Company', 'Lorem ipsum...', '2013-05-29 17:06:02', '2013-05-29 17:06:02', 2)
   (42.7ms)  COMMIT

现在当我在 conrtoller 上说时@comment = Comment.new(params[:comment])意味着它会寻找这条线"comment"=>{"content"=>"Lorem ipsum dolor sit amet, ..."}

这意味着 params[:comment] 在我的情况下是内容,参数作为散列发送,在你的情况下,你必须有类似的东西:

"user"=>{authenticity_token"=>"wI5sNQV/lkn8GtYv5YkRLZ6Hzx=", "username"=>"nonickname", "email"=>"no@bar.com", "password"=>"[FILTERED]", "confirm_password"=>"[FILTERED]"}`

在您的控制台日志中。我在您的控制台日志中查看了几次,并没有看到那里的用户, @user = User.new(params[:user])只要它丢失,您就无法调用。

关于表格,如果您使用的是:

<%= form_for @user, :html => {:class => 'form-horizontal'} do |f| %>

你可以用f这种方式来构建你的字段并少写

<%= form_for @user, :html => {:class => 'form-horizontal'} do |f| %>    
<div class = 'control-group'>
    <%= f.label :username, :html => {:class => 'control-label'} %>      
    <%= f.text_field :username, :placeholder => 'Username' %>
</div>
<div class = 'control-group'>
    <%= f.label :email, :html => {:class => 'control-label'} %>     
    <%= f.email_field :email, :placeholder => 'Email' %>
</div>
<div class = 'control-group'>
    <%= f.label :password, :html => {:class => 'control-label'} %>
    <%= f.password_field :passwor, :placeholder => 'Password' %>
</div>
<div class = 'control-group'>
    <%= f.label :confirm_password, :html => {:class => 'control-label'} %>
    <%= f.password_field :confirm_password, :placeholder => 'Confirm' %>
</div>
<%= f.submit 'Create', :class => 'btn btn-primary'%>
于 2013-05-29T17:16:34.243 回答
0

好的,解决了!

问题是引导表单,我需要设置名称属性:

  ...
  <div class = 'control-group'>
    <%= f.label :email, :html => {:class => 'control-label'} %>     
    <%= email_field_tag :email, nil, :name => 'user[email]', :placeholder => 'Email' %>
</div>
  ...

谢谢!

于 2013-05-29T17:28:35.423 回答