6
votes

basically I want to have two separate actions for change password and change email instead of just one.

I have updated my routes to point to my new controller which inherits from Devise::RegistrationsController.

My routes.rb:

devise_for :users, :controllers => { :registrations => "registrations" }

devise_scope :user do
  get "/users/password" => "registrations#change_password", :as => :change_password
end

My registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  def change_password
  end

end

My app/views/devise/registrations/change_password.html.erb

<%=debug resource%>

Which gives me nil.

What am I missing here?

Thanks!

2

2 Answers

10
votes

In Devise's built-in registrations_controller.rb, there is an authenticate_scope! method that creates the resource object you're looking for. It is executed by a prepend_before_filter, but only for certain methods:

class Devise::RegistrationsController < DeviseController
  ...
  prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]`

So you simply need to tell your custom controller to run that filter on your change_password method:

class RegistrationsController < Devise::RegistrationsController

  prepend_before_filter :authenticate_scope!, :only => [:change_password]

  def change_password
  end

end
-3
votes
class RegistrationsController < Devise::RegistrationsController

  def change_password
    super
    @resource = resource
  end
end

app/views/devise/registrations/change_password.html.erb

<%=debug @resource%>