【问题标题】:Rails: Devise Admin Retain Password on Editing UserRails:在编辑用户时设计管理员保留密码
【发布时间】:2014-10-15 15:00:10
【问题描述】:

我创建了新/编辑用户页面,以便管理员可以添加和更新用户。现在我正在尝试的是:当管理员编辑用户时,管理员可以选择不更新密码,只更新电子邮件。但是保存更新后的邮箱怎么保留密码呢?

编辑页面:

 <div><%= f.label :email, 'Email Address' %>
    <%= f.email_field :email, autofocus: true, :class => "form-control", autocomplete: "off", :placeholder => "Enter email address"  %></div><br />

  <div><%= f.label :password %>
    <%= f.password_field :password, autocomplete: "off", :placeholder => "Enter password"  %></div><br />

  <div><%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation, autocomplete: "off", :placeholder => "Confirm password"  %></div><br />

型号:

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation

控制器:

  def edit
  @user = User.find(params[:id])
  end

  def update
  @user = User.find(params[:id])

  if @user.update_attributes(params[:user])
    redirect_to edit_user_path
    flash[:notice] = "User updated."
  else
    render :action => 'edit'
  end
  end

【问题讨论】:

    标签: ruby-on-rails devise admin


    【解决方案1】:

    您可以检查密码字段是否为空,如果为空则使用devise的update_without_password方法,试试这个

    def update
      @user = User.find(params[:id])
      if params[:user][:password].blank?
        if @user.update_without_password(params[:user].except(:password, :password_confirmation))
          redirect_to edit_user_path
          flash[:notice] = "User updated."
        else
          render :action => 'edit'
        end
      else
        if @user.update_attributes(params[:user])
          redirect_to edit_user_path
          flash[:notice] = "User updated."
        else
          render :action => 'edit'
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多