未初始化的常量Users :: Auth :: Instagram :: CallbackController - 不确定哪个部分配置不正确

时间:2014-04-29 08:15:56

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

我正在使用 devise 并开始实施 omniauth-instagram。但是,我一直收到错误消息:

uninitialized constant Users::Auth::Instagram::CallbackController

我不确定我的配置中哪一部分不正确,所以我已经在我的routes.db,omniauth_callbacks_controllers.rb和devise.rb中摘录了一些摘录供参考。

非常感谢正确方向的任何一点!

routes.db
devise_for :users, :controllers => { :omniauth_callbacks => "users/auth/instagram/callbacks" }

/controllers/users/omniauth_callbacks_controllers.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def instagram

      # You need to implement the method below in your model (e.g. app/models/user.rb)
      @user = User.find_for_oauth(request.env["omniauth.auth"], current_users)

      if @user.persisted?
        sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
        set_flash_message(:notice, :success, :kind => "Instagram") if is_navigational_format?
      else
        session["devise.instagram_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end

  end

end

devise.rb
config.omniauth_path_prefix = 'users/auth/instagram/callbacks'

2 个答案:

答案 0 :(得分:0)

如果你看到你的devise.rb:

config.omniauth_path_prefix = 'users/auth/instagram/callbacks'

和您的routes.rb:

devise_for :users, :controllers => { :omniauth_callbacks => "users/auth/instagram/callbacks" }

都指向Users::Auth::Instagram::CallbacksController

虽然您已定义:

class Users::OmniauthCallbacksController

/controllers/users/omniauth_callbacks_controllers.rb

这应该是: /controllers/users/auth/instagram/callbacks_controllers.rb

并且您的班级应该更改为:

class Users::Auth::Instagram::CallbackController < Devise::OmniauthCallbacksController

或者从两个地方(routes.rb和devise.rb)中移除/auth并查看它是否有效?

答案 1 :(得分:0)

意识到我的命名惯例不正确!

this previous question的帮助下,我意识到我的文件名与路由和路径所指的类名不匹配。

例如,routes.rb和devise.rb都指路径:
"users/auth/instagram/callbacks"

我的班级文件名前后:
users/auth/instagram/omniauth_callbacks_controllers.rb
users/auth/instagram/callbacks_controllers.rb

同样重要的是要注意我的callbacks_controllers.rb中的类名也恰当地反映了文件的路径:
class Users::Auth::Instagram::CallbacksController