设计注册控制器的自定义操作无效资源 [英] custom action for devise registrations controller getting nil resource

查看:79
本文介绍了设计注册控制器的自定义操作无效资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



我的路线.rb:

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

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

我的registrations_controller.rb

  class RegistrationsController< Devise :: RegistrationsController 

def change_password
end

end

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

 <%= debug资源%GT; 

哪一个给我无。



什么我在这里错过吗?



谢谢!

解决方案

在Devise的内置 registrations_controller.rb ,有一个 authenticate_scope!方法创建您要查找的资源对象。它由 prepend_before_filter 执行,但仅适用于某些方法:

 类Devise ::注册控制器< 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


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!

解决方案

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

这篇关于设计注册控制器的自定义操作无效资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆