Omniauth “与” STI 和设计

Bla*_*San 2 ruby-on-rails devise omniauth

我想通了没有结果。我有一个名为 User 和 STI 粉丝和艺术家的模型,如下所示:

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable, :confirmable, :lockable,
     :recoverable, :rememberable, :trackable, :validatable, **:omniauthable**
end
Run Code Online (Sandbox Code Playgroud)

和我的其他模特

Class Artist < User end
Class Fan < User end
Run Code Online (Sandbox Code Playgroud)

我的路线

devise_for :users
devise_for :artists
devise_for :fans
Run Code Online (Sandbox Code Playgroud)

我在尝试运行我的服务器或其他任何东西时遇到问题,我收到此错误

Wrong OmniAuth configuration. If you are getting this exception, it means that either:

1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one
2) You are setting :omniauthable in more than one model
3) You changed your Devise routes/OmniAuth setting and haven't restarted your server
Run Code Online (Sandbox Code Playgroud)

我的应用程序很先进,不想回去重构它,任何帮助将不胜感激

Ash*_*aka 5

答案可以在这里找到。

由于您调用devise_for了三种不同的模型,其中一种正在使用该omniauthable模块,因此设计变得混乱。

任何一个:

  1. 删除devise_for:users.

  2. 或者omniauthable从用户模型中删除模块,创建您自己的 omniauth 路由并通过将您的 omniauth 配置移动到新文件中来停止使用设计的中间件。所以,不要把这个放在devise.rb

    Devise.setup do |config|
      config.omniauth :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
    end
    
    Run Code Online (Sandbox Code Playgroud)

    你现在在你的新文件中有这个omniauth.rb

    Rails.application.config.middleware.use OmniAuth::Builder do
      provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
    end
    
    Run Code Online (Sandbox Code Playgroud)

    Simple OmniAuth 上的 Railscast应该可以帮助您进行设置。