【问题标题】:Rails - Devise - Add action to controllerRails - 设计 - 向控制器添加动作
【发布时间】:2013-11-12 22:17:57
【问题描述】:

我在我的设计控制器中添加动作时遇到了一些麻烦。 我在文件夹users/ 中创建了一个名为registrations_controller 的控制器,如下所示:

class Users::RegistrationsController < Devise::RegistrationsController

还有,我的路线文件:

root to: 'home#index'

devise_for :users
devise_scope :users do
    get 'users/profil/:id', to: 'users/registrations#profil', as: 'profil'
end

我看到以下错误:

Unknown action
Could not find devise mapping for path "/users/profil/1"
This may happen for two reasons: 1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 2) You are testing a Devise controller bypassing the router.

我的代码有什么问题?

【问题讨论】:

    标签: ruby-on-rails devise controller


    【解决方案1】:

    尝试以下方法:

        devise_scope :user do
           get 'users/profil/:id', to: 'users/registrations#profil', as: 'profil'
        end
        devise_for :users, :controllers => {:registrations => "users/registrations"}
        resources :users 
    

    此外,您不需要将控制器命名为 Users::RegistrationsController 将其命名为 RegistrationController 保持继承与 Devise::RegistrationsController 一样

    【讨论】:

    • 什么意思 --> 为什么文件夹名不能写?我在此链接中强调了它的标准约定:github.com/plataformatec/devise/wiki/… 你会在他们的示例中看到他们不称他们的类为Confirmation::ConfirmationController &lt; Devise::ConfirmationsController 他们只是简单地称它为class ConfirmationsController &lt; Devise::ConfirmationsController
    • 抱歉之前的评论..我说得太快了..通过更改我的控制器的名称,我现在收到一个新错误:类 RegistrationsController 的超类不匹配
    • @user2975823 没关系。同样从这里突出显示:gist.github.com/kinopyo/2343176
    • 在你的例子中,他们不调用他们的类 Confirmation::ConfirmationController 因为 Confirmation 控制器位于文件夹中:app/controllers。但我的文件夹是:app/controllers/users/。这就是为什么我要通过 Users::... 来指定文件夹名称,对吗?
    • 只需在你的控制器app/controllers/registration_controller.rb 中使用它,在你的类的头部你只想要class RegistrationController &lt; Devise::RegistrationController
    猜你喜欢
    • 2016-08-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 2010-10-10
    相关资源
    最近更新 更多