设计注册控制器获取零资源的自定义操作

NS.*_*NS. 6 ruby-on-rails devise

基本上我想要有两个单独的操作来更改密码和更改电子邮件,而不只是一个.

我已经更新了我的路由以指向我的新控制器,它继承自Devise :: RegistrationsController.

我的routes.rb:

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

devise_scope :user do
  get "/users/password" => "registrations#change_password", :as => :change_password
end
Run Code Online (Sandbox Code Playgroud)

我的registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  def change_password
  end

end
Run Code Online (Sandbox Code Playgroud)

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

<%=debug resource%>
Run Code Online (Sandbox Code Playgroud)

这给了我零.

我在这里错过了什么?

谢谢!

Zac*_*Zac 10

在Devise的内置registrations_controller.rb中,有一个authenticate_scope!方法可以创建resource您正在寻找的对象.它由a执行prepend_before_filter,但仅适用于某些方法:

class Devise::RegistrationsController < DeviseController
  ...
  prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]`
Run Code Online (Sandbox Code Playgroud)

因此,您只需要告诉自定义控制器在您的change_password方法上运行该过滤器:

class RegistrationsController < Devise::RegistrationsController

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

  def change_password
  end

end
Run Code Online (Sandbox Code Playgroud)


Nic*_*aru -3

class RegistrationsController < Devise::RegistrationsController

  def change_password
    super
    @resource = resource
  end
end
Run Code Online (Sandbox Code Playgroud)

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

<%=debug @resource%>
Run Code Online (Sandbox Code Playgroud)