嗨,我有一个简单的应用程序,我使用 form_for 助手创建了一个表单。提交时,当我尝试在触发的操作中访问模型对象时,我没有正确获取其中的所有信息。一些属性变为空白。我已经花了几个小时来尝试调试问题,但无济于事。任何帮助将不胜感激。导轨 3.2 红宝石 1.9.2
表单代码是:signup.html.erb
<%= form_for (@user) , :url => { :action => "signup" ,:method=>"post"} do |u| %>
<p>
<%= u.label :login %>
<%= u.text_field :login ,:size => 20 %>
</p>
<p>
<%= u.label :password %>
<%= u.text_field :password,:size => 20 %>
</p>
<p>
<%= u.label :password_confirmation %>
<%= u.text_field :password_confirmation,:size => 20 %>
</p>
<p>
<%= u.label :email %>
<%= u.text_field :email ,:size => 20 %>
</p>
<%= u.submit %>
<% end %>
user_controller.rb 的“注册”操作的代码是
def signup
@user = User.new(params[:user])
if request.post?
if @user.save!
session[:user] = User.authenticate(@user.login, @user.password)
flash[:message] = "Signup successful"
redirect_to :action => "index"
else
flash[:warning] = "Signup unsuccessful"
end
end
end
当我单击提交按钮时,出现以下错误
ActiveRecord::RecordInvalid in UserController#signup
Validation failed: Password can't be blank, Password doesn't match confirmation
Rails.root: /Users/kabir/ror/mmeter1
Application Trace | Framework Trace | Full Trace
app/controllers/user_controller.rb:42:in `signup'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"yTC5i0HKltD4z40f0AQhir58CF9Pz+19VnKi3lvT6aE=",
"user"=>{"login"=>"abcde",
"password"=>"123456",
"password_confirmation"=>"123456",
"email"=>"abcde@kk.com"},
"commit"=>"Create User",
"method"=>"post"}
用户的模型验证是
validates_presence_of :login, :email, :password, :password_confirmation, :salt
validates_uniqueness_of :login, :email
validates_confirmation_of :password
在提交之前为表单生成的源代码是
<form accept-charset="UTF-8" action="/user/signup?method=post" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="yTC5i0HKltD4z40f0AQhir58CF9Pz+19VnKi3lvT6aE=" /></div>
<p>
<label for="user_login">Login</label>
<input id="user_login" name="user[login]" size="20" type="text" />
</p>
<p>
<label for="user_password">Password</label>
<input id="user_password" name="user[password]" size="20" type="text" />
</p>
<p>
<label for="user_password_confirmation">Password confirmation</label>
<input id="user_password_confirmation" name="user[password_confirmation]" size="20" type="text" />
</p>
<p>
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" size="20" type="text" />
</p>
<input name="commit" type="submit" value="Create User" />
</form>
我还在 user_controller 的注册操作中记录了@user 的值,它显示了以下值
The user params is {"login"=>"abcde", "password"=>"123456", "password_confirmation"=>"123456", "email"=>"abcde@kk.com"}
The user object is --- !ruby/object:User
attributes:
id:
username:
password:
created_at:
updated_at:
login: abcde
hashed_password: !binary |-
MzFiNDk4MGRmMTU2OTEwOThhNDdkYzNjOTZhOTFjYWFiOTVkN2NiOA==
email: abcde@kk.com
salt: tgCV8xIfxS
请注意,用户名和密码字段变为空白。任何帮助表示赞赏。
这是模型代码
class User < ActiveRecord::Base
# prevent the following fields from being updated through a post
# request. ie, these fields cannot be updated as a result of a form
# submittion. They can only be updated from within the model itself.
attr_protected :id, :salt
attr_accessible :login,:password,:password_confirmation,:email
# set the basic validation rules for the different attributes of the user
validates_length_of :login, :within => 3..40
# validates_length_of :password, :within => 5..40
validates_presence_of :login, :email, :password, :password_confirmation, :salt
validates_uniqueness_of :login, :email
validates_confirmation_of :password
# validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email"