6

现在,每次我更改用户/编辑表单中的任何内容时,都需要用户设置新密码。我希望它只需要输入当前密码(我怎样才能要求当前密码?)只有在输入新密码的情况下。我怎样才能做到这一点,非常感谢。

<%= form_for(@user, :html => {:multipart => true}) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.text_field :name, placeholder: :name %>
  <%= f.text_field :email, placeholder: :email %>
  <%= f.password_field :password, placeholder: "Enter new password" %>
  <%= f.password_field :password_confirmation, placeholder: "Confirm new password" %>
  <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
4

4 回答 4

8

使用来自 rails_has_elegance 和网络的一些信息,我想出了以下解决方案。

用户/编辑视图:

<%= form_for(@user, :html => {:multipart => true}) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.text_field :name, placeholder: :name %>
  <%= f.text_field :email, placeholder: :email %>
  <%= password_field_tag :current_password, params[:current_password], placeholder: "Current password" %>
  <%= f.password_field :password, placeholder: "New password (optional)" %>
  <%= f.password_field :password_confirmation, placeholder: "Confirm new password" %>
<% end %>

用户型号:

validates :password, :on => :create
validates :password_confirmation, presence: true, :on => :update, :unless => lambda{ |user| user.password.blank? }

用户控制器:

def update
  @user = User.find(params[:id])
  user = User.find_by_email(current_user.email).try(:authenticate, params[:current_password])
  if user && @user.update_attributes(params[:user])
    flash[:success] = "Profile updated"
    sign_in @user
    redirect_to @user
  else
    flash.now[:error] = "Incorrect Current Password" unless user
    sign_in @user
    render 'edit'
  end
end
于 2013-04-05T08:16:44.937 回答
2

您可以在表单中添加 old_password 字段

<%= f.password_field :old_password, placeholder: "Enter current password" %>

将其添加到attr_accessible :old_passwordattr_accessor :old_password

然后你可以验证它

validate :correct_old_pass, :on => :update

def correct_old_pass
  errors[:old_password] << 'Incorrect pass' if your_check_method
end
于 2013-04-04T07:53:23.340 回答
1

您可以创建一个单独的表单来更改密码。您可以像要求新密码一样要求当前密码:

<%= form_for(@user, :html => {:multipart => true}) do |f| %>
  <%= f.password_field :password, placeholder: "Enter current password" %>
  <%= f.password_field :password, placeholder: "Enter new password" %>
  <%= f.password_field :password_confirmation, placeholder: "Confirm new password" %>
  <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
于 2013-04-04T07:42:55.553 回答
1

我建议在您的 users_controller 中为此进行额外的 edit_password 和 update_password 操作:

@user = User.find(params[:id])
user = User.find_by_email(current_user.email).try(:authenticate, params[:current_password])
if user && @user.update_attributes(params[:user])
  flash[:success] = "Password updated!"
  (sign_in @user)
  redirect_to ...

在您的表单中,只需使用以下字段:

  <%= label_tag :current_password, "Current password:" %>
  <%= password_field_tag :current_password, params[:current_password] %>

  <%= form.label :password, "New password:" %>
  <%= form.password_field :password %>

  <%= form.label :password_confirmation, "Confirm new password" %>
  <%= form.password_field :password_confirmation %>
于 2013-04-04T08:05:41.860 回答