簡體   English   中英

覆蓋Rails可安裝引擎中的設備注冊控制器

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

我在Rails引擎( my_app/components/base )中使用設計並覆蓋注冊控制器,以便在成功注冊后將用戶轉發到另一個頁面。 如果我啟動my_app ,這很有效,但無論出於何種原因,當從為測試引擎而生成的spec/dummy應用程序啟動引擎時,設計不會使用我的自定義注冊控制器。

使用設計的引擎在main_app/components/base

我的自定義控制器是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

引擎的路由文件是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顯示控制器被識別:

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

但是,自定義注冊控制器不會在虛擬應用程序中使用,因此無法進行RSpec測試。 如果我以交互方式使用虛擬應用程序,情況也會如此。

問題解決了。 實際上問題不是沒有調用自定義注冊控制器,但我覆蓋了錯誤的回調函數。

我的意圖是在注冊后轉發到“感謝您注冊”頁面,但由於我的用戶模型已確認已注冊,但在用戶確認其帳戶之前仍處於非活動狀態。 所以正確的回調函數是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