我正在使用blowfish gem为我的用户加密密码(user
模型)。Rails用blowfish丢失参数密码
模式中的我没有password
场了,但在轨道控制台我可以(而且必须),以便能够调用user.save
运行user.password = "xxx"
和user.password_confirmation = "xxx"
。这在rails控制台中工作,但我有一个webform,用户在逻辑上能够编辑他/她的密码。
这是我edit.html.erb
<%= form_for(@user) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<%= submit_tag("Edit User") %>
<% end %>
在_form.html.erb
,涉及到密码parital是这个
<table>
...
<tr>
<th>Password</th>
<td><%= f.text_field(:password) %></td>
</tr>
<tr>
<th>Confirm Password</th>
<td><%= f.text_field(:password_confirmation)%></td>
</tr>
在我users_controller.rb
我需要login password password_confirmation
这样
def update
@user = User.find(params[:id])
@user.update_attributes(user_params)
if @user.save
flash[:notice] = "Update Successful"
redirect_to(:action => 'show', :id => @user.id)
else
flash[:notice] = "Error Updating"
render('edit')
end
end
和
私人
def user_params
r = params.require(:user)
r.require(:login)
r.require(:password)
r.require(:password_confirmation)
r.permit(:first_name, :last_name, :login, :password, :password_confirmation)
end
的问题,不是我提交完整的形式,即更新的罚款。问题是,当我离开密码字段为空,而不是再次渲染编辑形式,它给了我一个Action Controller: Exception
param not found: password
并指向r.require(:password)
线user_params
功能
编辑
我评论这两个要求的行并验证模型中是否存在登录名,密码,password_confirmation。但是现在我得到这个错误
undefined method
user'for#pointing to the
@ user.upadte_attributes(user_params)`line。
我仍然需要:user
然后.permit(.....)
为Rails 4中的强参数吧?
EDIT 2 - 在users_controller.rb
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:notice] = "Update Successful"
redirect_to(user_path(@user.id))
else
flash[:notice] = "Error Updating"
render('edit')
end
end
和user params
私人
def user_params
params.require(:user).permit(:first_name, :last_name, :login, :password, :password_confirmation, :position, :pictureString)
end
错误信息更新方法:
undefined method `user' for #<User:0x007f4d482b1af0>
Extracted source (around line #36):
34 def update
35 @user = User.find(params[:id])
36 if @user.update_attributes(user_params)
37 flash[:notice] = "Update Successful"
38 redirect_to(user_path(@user.id))
39 else
app/controllers/users_controller.rb:36:in `update'
编辑
一些进一步的调查表明这一点:
如果我离开了.permit(....)
出user_params
功能(即它只读params[:user]
),那么我没有得到未定义的方法错误,但预期的forbidden attributes
错误。也许这可以帮助你找到问题所在。
你得到的错误,因为这一行:'r.require(:password)' - 所以当然如果它是空的,你会得到这个消息。如果您尝试验证是否存在密码属性,则应在模型中完成。 – veritas1
谢谢,我正在关注的教程把需求放在控制器中,而不是模型 – Killerpixler
酷,所以你已经回答了你自己的问题。 – veritas1