简体   繁体   中英

How i can change password of a user with custom method using Devise

I am using rails 3 with gem devise.Devise has built in method for change password.But i want

that admin can reset password of any user and how i can do it please help..

You can define custom method and also define routes for it

 <%= form_for(:user, :url => {:controller => '/administrator', :action =>                  'change_password',:id => @user.id}, :html => {:id => 'admin_update_form',:method => :put}) do |f| %>

<div class="change-pass">
  <h2>Change Passwords</h2>

  <div class="pass-detail">
    <div class="username-btn">
      <label>Passwords</label>
      <%= f.password_field :password, :class=> 'fields' %>
    </div>
    <div class="username-btn" style="margin-bottom:40px;">
      <label>Confirm Passwords</label>
      <%= f.password_field :password_confirmation, :class=> 'fields' %>
    </div>
    <%=hidden_field_tag 'user_ids',:id => 'user_ids'  %>
    <div class="username-btn_button">
      <%= f.submit 'change password', :id => 'password_btn' ,:class => 'reg-findjob'%>
    </div>
  </div>
</div>

In controller code

def change_password
   @user = User.find(params[:id])
   if @user.update_attributes(:password => params[:user][:password],    :password_confirmation => params[:user][:password_confirmation])
     flash[:notice] = 'Password was changed successfully.'
     render :text => 'ok'
   else
    flash[:error]=[]
    @user.errors.full_messages.each do |error|
      flash[:error] << error
    end       
end

In controller find user and update its password attribute.I am sending user id with form

whose password is to changed or you can do it according your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM