简体   繁体   中英

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. 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.

The engine that uses devise is under main_app/components/base

My custom controller is 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

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:

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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