简体   繁体   English

覆盖Rails可安装引擎中的设备注册控制器

[英]Overriding the devise registrations controller in a Rails mountable engine

I'm using devise inside a Rails engine ( my_app/components/base ) and override the registrations controller in order to forward the user to another page upon successful signup. 我在Rails引擎( my_app/components/base )中使用设计并覆盖注册控制器,以便在成功注册后将用户转发到另一个页面。 This works quite well if I launch my_app , but for whatever reason devise doesn't use my custom registrations controller when launching the engine from the spec/dummy application that is generated for testing the engine. 如果我启动my_app ,这很有效,但无论出于何种原因,当从为测试引擎而生成的spec/dummy应用程序启动引擎时,设计不会使用我的自定义注册控制器。

The engine that uses devise is under main_app/components/base 使用设计的引擎在main_app/components/base

My custom controller is main_app/components/base/app/controllers/base/users/registrations_controller.rb 我的自定义控制器是main_app/components/base/app/controllers/base/users/registrations_controller.rb

module Base
  class Users::RegistrationsController < Devise::RegistrationsController
    layout 'base/application'

    def after_sign_up_path_for(user)
      fail # note: won't get called
      base.welcome_new_user_path(user)
    end    
  end
end

The engine's routes file is main_app/components/base/config/routes.rb 引擎的路由文件是main_app/components/base/config/routes.rb

Base::Engine.routes.draw do

  devise_for :users, {
    class_name: "Base::User",
    module: :devise,
    controllers: { registrations: 'base/users/registrations' }
  }
  get 'users/welcome_new_user/:id' => "users/welcome#show", as: :welcome_new_user
end

rake routes shows that the controller gets recognized: rake routes显示控制器被识别:

cancel_user_registration GET    /users/cancel(.:format)               base/users/registrations#cancel
       user_registration POST   /users(.:format)                      base/users/registrations#create
   new_user_registration GET    /users/sign_up(.:format)              base/users/registrations#new
  edit_user_registration GET    /users/edit(.:format)                 base/users/registrations#edit
                         PATCH  /users(.:format)                      base/users/registrations#update
                         PUT    /users(.:format)                      base/users/registrations#update
                         DELETE /users(.:format)                      base/users/registrations#destroy
       user_confirmation POST   /users/confirmation(.:format)         devise/confirmations#create
   new_user_confirmation GET    /users/confirmation/new(.:format)     devise/confirmations#new
                         GET    /users/confirmation(.:format)         devise/confirmations#show
        welcome_new_user GET    /users/welcome_new_user/:id(.:format) base/users/welcome#show

However, the custom registrations controller won't get used in the dummy app, thus failing my RSpec tests. 但是,自定义注册控制器不会在虚拟应用程序中使用,因此无法进行RSpec测试。 The same happens if I interactively use the dummy app. 如果我以交互方式使用虚拟应用程序,情况也会如此。

Problem solved. 问题解决了。 Actually the issue was not that the custom registrations controller was not called, but I overrode the wrong callback function. 实际上问题不是没有调用自定义注册控制器,但我覆盖了错误的回调函数。

My intention was to forward to a 'thank you for registering' page after signup, but as my user model is confirmable it's registered, but still inactive until the user confirms his account. 我的意图是在注册后转发到“感谢您注册”页面,但由于我的用户模型已确认已注册,但在用户确认其帐户之前仍处于非活动状态。 So the correct callback function is after_inactive_sign_up_path_for and not after_sign_up_path_for . 所以正确的回调函数是after_inactive_sign_up_path_for而不是after_sign_up_path_for

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM