Vasseurth
Vasseurth

Reputation: 6496

Devise custom password changing

As the title states, I am using devise. I came upon this link that describes the process of doing this. I have this users controller:

    def update_password
      @user = User.find(current_user.id)
      #raise @user.to_yaml
      if @user.update_attributes(params[:user])
        # Sign in the user by passing validation in case his password changed
        sign_in @user, :bypass => true
        #flash[:notice] = "Password Successfully Changed"
        redirect_to settings_path
      else
        flash[:notice] = @user.errors.full_messages
        render password_path
      end
    end

Ignore the #raise @user.to_yaml. And this view:

        <h1>Change Password</h1>

        <%= form_for(:user, :url => { :action => 'update_password'}) do |f| %>
            <div>
                <%= f.label :current_password %>
                <%= f.password_field :current_password %>
            </div>

            <div>
                <%= f.label :password %>
                <%= password_field_tag :password %>
            </div>

            <div>
                <%= f.label :password_confirmation %>
                <%= f.password_field :password_confirmation %>
            </div>

          <div>
                <%= f.submit "Change Password" %>
            </div>
        <% end %>

I have that view mapped to this controller method :

   def password
      @user = current_user
    end

To separate the action and the view, and my config/routes.rb is this: match '/password' => 'users#password', :as => 'password'

The problem arises when I click on the "Change Password" button from the form. This is the link it brings me to: "http://localhost:3000/assets?action=update_password&controller=users" and I get the error "Couldn't find User with id=assets"

I have no idea as to why its acting in this manner and what I'm doing wrong because I have copied what the webpage said. Has anyone had any experience with this and can aid me? Thanks!

Upvotes: 2

Views: 2256

Answers (1)

patrickmcgraw
patrickmcgraw

Reputation: 2495

If you are using Rails 3 or newer you could setup your routes using the resource syntax:

resources :users do
  member do # These routes will apply to specific model instances
    get 'password' # /users/:id/password
    put 'update_password' # /users/:id/updated_password
  end
end

then you would be able to use a path helper in your form_for declaration

<%= form_for @user, :url => update_password_user_path(@user) do |form| %>
...
<% end %>

Upvotes: 1

Related Questions