【问题标题】:How to set up devise for 2 models using engines如何使用引擎为 2 个模型设置设计
【发布时间】:2018-06-12 02:52:01
【问题描述】:

我在使用 Engines 正确设置设计时遇到了一些麻烦。

在我的引擎中,我有:

config/initializers/devise.rb

config.router_name = :portal_core

config/routes.rb

devise_for :users, class_name: 'PortalCore::User', module: :devise

app/models/portal_core/user.rb

module PortalCore
  class User < ApplicationRecord
    ...
    devise :database_authenticatable, :trackable, :confirmable,
         :recoverable, :rememberable, :validatable, :lockable
  end
end

我所有的测试都在这里通过。 然后,在我的 host 应用程序之一中:

config/routes.rb

devise_for :admin_users, class_name: 'AdminUser'

app/models/admin_user.rb

  class User < ApplicationRecord
    ...
    devise :database_authenticatable, :trackable,
     :recoverable, :rememberable, :validatable
  end

当我在宿主应用程序中运行此测试时:

context "When not logged in" do
  it "redirect to new user session when try to access index" do
    process :index, method: :get, params:{}
    expect(response).to redirect_to(new_admin_user_session_path)
  end
end

我收到了这个错误:NoMethodError: # Devise::FailureApp:0x0000000008c9f1f0>

的未定义方法“portal_core”

如果我在宿主应用中安装引擎路由:

mount PortalCore::Engine => '/portal_core'

然后我得到了这个错误:预期的响应是重定向到http://test.host/admin_users/sign_in,但重定向到http://test.host/

如果我尝试启动服务器,它会一直重定向 401 Unauthorized

可能缺少某些配置,但我不知道是什么。

【问题讨论】:

    标签: ruby-on-rails devise rails-engines


    【解决方案1】:

    所以,我不知道这是解决方案还是解决方法。

    在主机应用程序设计初始化程序中,我将 router_name 设置为 nil: config.router_name = nil 它奏效了。

    欢迎任何其他解决方案!

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 2013-05-06
      • 2010-09-08
      相关资源
      最近更新 更多