Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom action for devise registrations controller getting nil resource

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!

like image 859
NS. Avatar asked Jun 29 '12 01:06

NS.


1 Answers

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
like image 64
Zac Avatar answered Nov 03 '22 18:11

Zac