如何更改current_user密码?

时间:2015-10-06 02:11:10

标签: ruby-on-rails

我试图让用户在网站上编辑他/她的个人资料,除了密码更新之外我完成了大部分工作。

这是我用于密码更新的表单:

<%= hidden_field_tag 'attribute', 'password', id: nil %>

正如您所看到的,我还有<%= hidden_field_tag 'attribute', 'email', id: nil %>,我用它来确定,我提交的是哪种形式。因为我在页面中有更多的表单。例如,电子邮件更新表单,并且该表单具有 def update @profile = User.find_by_id(current_user.id) if params[:attribute] == 'email' if @profile.update_attributes(email_parameter) redirect_to request.referrer else render 'edit' end elsif params[:attribute] == 'password' if User.password_match?(params[:current_password]) else render 'edit' end end end 。到目前为止,这里没有问题。

这是我在profile_controller中的更新方法:

  def self.password_match?(entered_password = '')
    password == User.hash_with_salt(entered_password, salt)
  end

电子邮件,部分工作正常,但我必须让密码部分工作。

我的user.rb模型中有这个,我用它进行身份验证。

undefined local variable or method `password'

正如你所看到的,我试图用我的方法来调用它。因为我必须检查用户是否输入了current_password字段。

但是当我提交密码更新表单时,我收到此错误消息:

{{1}}

我不知道该怎么做。

修改

这个问题在这里得到解答:Params are empty after form submission

2 个答案:

答案 0 :(得分:1)

由于您已在User模型

中定义了实例方法
def password_match?(entered_password = '')
    password == User.hash_with_salt(entered_password, salt)
end

此方法仅适用于@profile = User.find_by_id(current_user.id)这里的profile实例,User将是@profile类的实例,因此,User可以使用该方法def update @profile = User.find_by_id(current_user.id) . . . elsif params[:attribute] == 'password' if @profile.password_match?(params[:current_password]) else render 'edit' end end end Error:A problem occurred configuring project ':app'. >Could not resolve all dependencies for configuration ':app:_debugUnitTestCompile'. > Could not resolve junit:junit:4.12. Required by: prototype1:app:unspecified > Could not resolve junit:junit:4.12. > Could not get resource 'jcenter.bintray.com/junit/junit/4.12/junit-4.12.pom'. > Could not GET 'jcenter.bintray.com/junit/junit/4.12/junit-4.12.pom'. > Connection to jcenter.bintray.com refused 1}} 所以,

s = "1234567"
sum([int(character) for character in s])

有关实例和类方法的更多信息,请参阅 http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/ https://cbabhusal.wordpress.com/2015/08/05/ruby-various-ways-to-define-class-methods/

答案 1 :(得分:1)

用户控制器:

def update
  @profile = User.find_by_id(current_user.id)

  if params[:attribute] == 'email'
    if @profile.update_attributes(email_parameter)
      redirect_to request.referrer
    else
      render 'edit'
    end
  elsif params[:attribute] == 'password'
    if @profile.password_match?(params[:current_password])
       if @profile.update_attributes(password_parameter)
         # redirect_to #...
       else
         render 'edit', notice: 'Failed update password, password too short or whatever'
       end
    else
      render 'edit', notice: 'Please enter your current password / Current password is incorrect'
    end
  end
end

private
def password_parameter
  # code
end

用户模型:

def password_match?(entered_password = '')
  self.password == User.hash_with_salt(entered_password, self.salt)
end

# I assumed the password and salt method is already defined
# you have to define it first if it is not defined
def password
   # ...
end

def salt
   # ...
end