Ruby on rails 如何更改当前用户密码?

Ruby on rails 如何更改当前用户密码?,ruby-on-rails,Ruby On Rails,我试图让用户编辑他/她的个人资料在网站上,我完成了大部分的部分,除了密码更新 这是我用于更新密码的表单: <div class="title">Change Password</div> <div class="password"> <%= profile_error_messages_for(@profile) %> <%= form_for :user, url: change_password_path do |f| %>

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

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

<div class="title">Change Password</div>
<div class="password">
  <%= profile_error_messages_for(@profile) %>

  <%= form_for :user, url: change_password_path do |f| %>
      <%= hidden_field_tag 'attribute', 'password', id: nil %>
      <div class="input-box">
        <%= label_tag(:current_password, 'Current Password') %>
        <%= f.password_field :current_password, class: 'form-control input-md input-width-large' %>
      </div>
      <div class="input-box">
        <%= label_tag(:password, 'New Password') %>
        <%= f.password_field :password, class: 'form-control input-md input-width-large' %>
      </div>
      <div class="input-box">
        <%= label_tag(:password_confirmation, 'Password Confirmation') %>
        <%= f.password_field :password_confirmation, class: 'form-control input-md input-width-large' %>
      </div>
      <%= submit_tag 'Change Password', class: 'btn btn-success btn-md input-width-large' %>
  <% end %>
</div>
电子邮件,部分工作正常,但我必须让密码部分工作

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

  def self.password_match?(entered_password = '')
    password == User.hash_with_salt(entered_password, salt)
  end
正如你所看到的,我试图从我的方法中调用这个。因为我必须检查用户输入的当前密码字段是否正确

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

undefined local variable or method `password'
我不知道该怎么办

编辑


这里回答了这个问题:

因为您已经在
用户
模型中定义了一个实例方法

def password_match?(entered_password = '')
    password == User.hash_with_salt(entered_password, salt)
end
此方法仅适用于实例,例如
@profile=User。按id查找(当前用户id)
此处
profile
将是
User
类的实例,因此该方法将适用于
@profile
而不是
User
所以

有关实例和类方法的更多信息,请参见 用户控制器:

 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
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

你在用devise吗?@AntarrByrd没有。我自己做的。你试过用它作为实例方法吗?我对ruby有点陌生。用它作为实例方法是什么意思?当你说instance时,我只知道@variable。请发布完整的代码和错误日志,看起来我们在正确的轨道上,但当我在表单中输入正确的密码时,它不会出现在if语句中。我的想法是,在“密码匹配”中,我接受密码来检查密码是否匹配。但在您的示例中,我将all对象发送到“password_match?”。如何仅提交密码?我尝试了以下方法:if@profile.password.password\u match?(params[:current\u password]),但我再次遇到了这个错误:未定义的方法“password\u match?”我猜
@profile.password
返回一个字符串,因此它不应该响应
password\u match?
。确保您已在
public
范围中定义了方法
password\u match?
;i、 e.不要像我建议的那样在
私有/受保护的
关键字之后尝试;错误
未定义的局部变量或方法密码“
不应出现。。
密码
方法将由
用户
@profile
objectI中的实例响应,该实例发现问题所在,但仍在尝试修复。密码表单不传递参数。我尝试在“password_match”(密码匹配)中键入“put password_entered”(输入密码),当我提交表单时,它不会显示任何内容,但当我登录网站时,它会在那里显示密码。您的方法似乎是正确的,但不知怎的,如果@profile.password_match?(参数[:current_password])没有返回true。即使密码匹配?这是正确的。因为它与登录一起工作。我假设有3个密码字段,第一个是原始密码字段,因此用户必须首先输入其旧密码,然后授权用户更改其密码,接下来的2个字段是“密码”和“密码确认”字段,因此密码和密码验证必须包含相等的值,如果您希望@profile.password\u match?(params[:current\u password])不返回true,这意味着用户输入了不正确的旧密码,我知道,我尝试了正确的密码,但仍然无效。数据库中的密码是否已哈希?如果是,请检查您使用的盐是否正确?
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