2014-02-21 39 views
0

我正在使用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: Exceptionparam 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错误。也许这可以帮助你找到问题所在。

+0

你得到的错误,因为这一行:'r.require(:password)' - 所以当然如果它是空的,你会得到这个消息。如果您尝试验证是否存在密码属性,则应在模型中完成。 – veritas1

+0

谢谢,我正在关注的教程把需求放在控制器中,而不是模型 – Killerpixler

+0

酷,所以你已经回答了你自己的问题。 – veritas1

回答

0

强参数是而不是用于表单验证。它们是为了安全目的而取代在模型级别使用的宏attr_accessible

表单验证既可以在客户端执行,也可以通过将参数传递给执行验证的某个模型(如果认为有效,则会调用update_attributes)。

+0

验证在模型中......问题是为什么params.require.permit不再工作了......它是否与RESTful路由有关? – Killerpixler

+0

请发布完整的stacktrace错误信息(或者更新已发布的代码) - 未定义的方法'user''不能从您发布的代码中提取,因为您没有在代码中的任何地方调用'user' 。 – PinnyM

+0

我发布了错误消息。我认为这可能与RESTful路由有关,因为我在一段时间内没有改变这些行,并且他们总是习惯于工作。我只是无法弄清楚错误意味着什么......我没有看到'用户'用作任何地方的方法 – Killerpixler