1
votes

I'm using devise on a Rails application and for various reasons I needed to add a custom password change, using solution 3 from here: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password

I've looked through various other questions and tried augmenting my routes in a number of ways but the url always comes up /users/update_password (without a parameter passed), or /users/update_password.254 (with a parameter passed). I need it to be /users/254/update_password.

Here is the Users Controller I'm working with (relevant methods are update_password and user_params).

class UsersController < ApplicationController
inherit_resources
load_and_authorize_resource
before_filter :authenticate_user!

def destroy
  name = @user.to_s
  destroy!(notice: "User #{@user.to_s} was deleted.")
end

def edit
  @user = current_user
end

def update_password
  @user = User.find(current_user.id)
  if @user.update_with_password(user_params)
    sign_in @user, :bypass => true
    redirect_to root_path
  else
     render "edit"
 end
end

private

def build_resource_params
  [params.fetch(:user, {}).permit(:name, :email, :phone_number, :password, :password_confirmation).tap do |p|
    p[:institution_pid] = build_institution_pid if params[:user][:institution_pid]
    p[:role_ids] = build_role_ids if params[:user][:role_ids]
  end]
end

def build_institution_pid
  institution = Institution.find(params[:user][:institution_pid])
  authorize!(:add_user, institution)
  institution.id
end

def build_role_ids
  [].tap do |role_ids|
    roles = Role.find(params[:user][:role_ids].reject &:blank?)
    roles.each do |role|
      authorize!(:add_user, role)
      role_ids << role.id
    end
  end
end

def user_params
  params.required(:user).permit(:password, :password_confirmation, :current_password)
end

end

This is the relevant portion of routes.rb

devise_for :users

resources :users do
  collection do
    get 'password'
    put 'update_password'
  end
end

This is the view I created to go with it:

<div class="page-header">
  <h1>Change Password</h1>
</div>

<%= form_for(@user, :url => { :action => "update_password" } ) do |f| %>
  <div class="field">
  <%= f.label :password, "Password" %><br />
  <%= f.password_field :password, :autocomplete => "off"  %>
</div>
<div class="field">
  <%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %>
</div>
<div class="field">
  <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
  <%= f.password_field :current_password %>
</div>
<div class="action_container">
  <%= f.submit %>
</div>

And this is the link: (currently with a parameter but I've tried without as well)

<li><%= link_to "Change Password", update_password_users_path(current_user) %></li>

When I click that link I get routed to a page that says ActiveRecord::RecordNotFound in UsersController#show, Couldn't find User with id=update_password.

I tried to follow the instructions pretty closely and I've tried to fiddle with the routes a couple times to get it to work and I'm pretty lost at this point. Has anyone seen this before and can help? Thanks!

1

1 Answers

1
votes

To achieve what you need, you will have the following routes:

resources :users do
  patch 'update_password', on: :collection
  get 'edit_password', on: :member
end

Then you'll add a link that would lead user to update password form. The link should look like as follows:

<li><%= link_to "Change Password", edit_password_user_path(current_user) %></li>

The link would lead to users/edit_password.html.erb:

<div class="page-header">
  <h1>Change Password</h1>
</div>

<%= form_for(@user, :url => { :action => "update_password" } ) do |f| %>
  <div class="field">
  <%= f.label :password, "Password" %><br />
  <%= f.password_field :password, :autocomplete => "off"  %>
</div>
<div class="field">
  <%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %>
</div>
<div class="field">
  <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
  <%= f.password_field :current_password %>
</div>
<div class="action_container">
  <%= f.submit %>
</div>
<% end %>

When form is submitted, that would trigger update_password in UsersController. Here is the code for controller:

class UsersController < ApplicationController

    def edit_password
        @user = current_user
    end


    def update_password
        @user = User.find(current_user.id)
        if @user.update_with_password(user_params)
            sign_in @user, :bypass => true
            redirect_to root_url
        else
            render "edit_password"
        end
    end

    private

    def user_params
        params.required(:user).permit(:password, :password_confirmation, :current_password)
    end

end