我正在使用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 域(我打算修复它),但这似乎不是问题!
谢谢你的帮助!