Question

In my ruby on rails project, I am using devise as the authentication plugin. Now I have added the change password functionality to the project and when submit the form, I'm getting an error No route matches "/users/update_password"

This is my update password code

  def update_password
    @user = User.find(current_user.id)
    if @user.update_attributes(params[:user])
      # Sign in the user by passing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to root_path
    else
      render "edit"
    end
  end

This is my route details

resources :users, :only => [] do
        collection do
            get 'current'
            get 'edit'
            post 'update_password'
        end
    end

and this is my edit form

<div id="loginForm">
    <div id="loginHeader"><h3>Change Password</h3></div>
    <div id="loginContent">
        <%= form_for(@user, :url => { :action => "update_password" }) do |f| %>
            <p><%= f.password_field :current_password, :class => "login_txt", :placeholder => "We need your current password to confirm your changes", :autocomplete => "off" %></p>
            <p><%= f.password_field :password, :class => "login_txt", :placeholder => "New password", :autocomplete => "off"  %></p>
            <p><%= f.password_field :password_confirmation, :class => "login_txt", :placeholder => "Confirm password", :autocomplete => "off" %></p>
            <p>
                <input id="user_submit" name="commit" type="submit" value="Update Password" class="login_btn" /> 
            </p>
        <% end %>
    </div>
</div>

Can anyone help me how to fix this issue

Thanks a lot

Was it helpful?

Solution

In your routes.rb you define update_password as a POST action on the collection (/users/update_password), but in your form you are implicitly defining it as a PUT action when you pass @user to form_for, since @user already exists.

I haven't tested this, but I believe that changing your route on the collection from a POST to a PUT should fix this:

resources :users, :only => [] do
  collection do
    get 'current'
    get 'edit'
    put 'update_password'
  end
end

See also: Rails 3.0.10, customer routes, post and form_for tag

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top