2012-06-29 46 views
6

基本上我想要有两个单独的操作来更改密码和更改电子邮件,而不是一个。设计注册的自定义操作控制器获取无资源

我已经更新了我的路线,指向从Devise :: RegistrationsController继承的我的新控制器。

我的routes.rb:

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

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

我registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController 

    def change_password 
    end 

end 

我的应用程序/视图/设计/注册/ change_password.html.erb

<%=debug resource%> 

其中给出我没有。

我在这里错过了什么?

谢谢!

回答

-3
class RegistrationsController < Devise::RegistrationsController 

    def change_password 
    super 
    @resource = resource 
    end 
end 

应用程序/视图/设计/注册/ change_password.html.erb

<%=debug @resource%> 
+0

我测试了这一点,它没't不适合我,因为'Devise :: RegistrationsController'超类没有'super'关键字引用的'change_password'方法。 – Zac

10

在设计的内置registrations_controller.rb,有一个authenticate_scope!方法创建你正在寻找的resource对象。它是由一个prepend_before_filter执行,但仅适用于特定的方法:

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

所以,你只需要告诉您的自定义控制器上的change_password方法运行过滤器:

class RegistrationsController < Devise::RegistrationsController 

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

    def change_password 
    end 

end 
+1

我想你需要追加':change_password'动作到那些默认的'[:edit,:update,:destroy]'而不是只指定':change_password'。在我的类似案例中,我有一个名为':finish'的动作,如果我指定'only::finish',那么'resource'对'edit'动作是'nil' – chaimann

相关问题