13

我已经按照本教程(http://railscasts.com/episodes/236-omniauth-part-2)使用 OmniAuth 和 Devise 创建 facebook 登录,但出现此错误:在我的路线中自动加载常量用户时检测到循环依赖。 rb

  devise_for :users , :controllers => {:registrations => 'registrations'}

注册控制器.rb

Class RegistrationsController < Devise::RegistrationsController

  def create
    super
    session[:omniauth] = nil unless @user.new_record?
  end

  private

  def build_resource(*args)
    super
    if session["devise.omniauth"]
      @user.apply_omniauth(session["devise.omniauth"])
      session["devise.omniauth"] = nil
   end
  end
end

这是我来自 AuthenticationsController 的创建方法

def create
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:notice] = "Signed in successfully."
      sign_in_and_redirect(:user, authentication.user)
    elsif current_user
      current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
      flash[:notice] = "Authentication successful."
      redirect_to authentications_url
    else
      user = User.new
      user.apply_omniauth(omniauth)
      if user.save
        flash[:notice] = "Signed in successfully."
        sign_in_and_redirect(:user, user)
      else
        session[:omniauth] = omniauth.except('extra')
        redirect_to new_user_registration_url
      end
    end
end
4

8 回答 8

12

registrations_controller.rb被救到哪里去了?位置很重要。我发现我在将其保存到app/controllers/devise/.. 它只需要保存在app/controllers/.例如:

app/controllers/registrations_controller.rb


此外,config/routes.rb路线应定义为:

devise_for :users, controllers: { registrations: 'registrations' }

于 2014-07-05T17:35:54.863 回答
2

我对 lib 中的某些类有同样的问题(使用过config.autoload_paths += Dir["#{config.root}/lib/**/"]

对我来说,帮助将轨道4.0.0.rc14.0.0

于 2013-09-03T18:58:00.407 回答
2

好吧,在我的 development.rb 中添加以下行后,我得到了解脱

config.middleware.delete Rack::Lock

参考: https ://github.com/websocket-rails/websocket-rails/issues/101

你终于可以试试这个了。

于 2013-09-17T19:11:04.893 回答
2

我有一个类似的问题。

然后意识到我在控制器内的不同文件夹中复制了相同的文件,这就是问题所在。

我有两个文件具有相同的内容:

  app/controllers/logs_controller.rb
  app/controllers/api/logs_controller.rb
于 2014-02-14T02:40:05.043 回答
0

我用错字创建了同样的错误,我有

module EmployeeReminderssHelper

调用帮助文件时

employee_reminders_helper.rb

(注意额外的's')

于 2015-01-16T21:41:12.100 回答
0

我发现这在 development.rb 中有效:

config.reload_classes_only_on_change = false

(我之前尝试过删除 Gemfile.lock 并运行包更新,以及更改 Rails 版本,如此处和其他地方所述。它们对我不起作用。)

于 2014-01-21T12:48:00.320 回答
0

许多 gem 开始在 rails 4 上中断,这都是由于控制器中的 unloadable 问题。 https://github.com/flyerhzm/switch_user/issues/34 https://github.com/thoughtbot/high_voltage/issues/68 https://github.com/thoughtbot/clearance/issues/276 等等

您应该查看哪个 gem 造成问题的错误。一旦您知道:1)检查该 gem 的未解决问题 2)如果该问题存在并已修复,请确保您已修复或更新 gem。3)如果没有,您可以创建一个问题并要求他们修复它。4)如果您不想等待他们的修复,您可以形成 gem 并为其推送修复 https://github.com/cashins/email_preview/commit/b34a077a954b98bd086153fae8979ad142667555 所有修复都是相同的(从指定的控制器中删除 unloadable )

希望能帮助到你。

如果没有任何帮助降级您的 Rails 版本。

于 2013-08-02T10:17:15.257 回答
-1

设计 wiki 对这个话题有这样的说法:

参考:https ://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in#preventing-redirect-loops

防止重定向循环

因为上面的 after_sign_in_path_for 代码只检查 request.referer == sign_in_url,这些方法(调用 after_sign_in_path_for)也必须被覆盖(否则你会遇到重定向循环):

Devise::PasswordsController#after_resetting_password_path_for
Devise::RegistrationsController#after_sign_up_path_for
Devise::RegistrationsController#after_update_path_for

这可以这样做:

# routes.rb
devise_for :users, controllers: { registrations: 'users/registrations', passwords: 'users/passwords' }

# users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
  protected
    def after_sign_up_path_for(resource)
      signed_in_root_path(resource)
    end

    def after_update_path_for(resource)
      signed_in_root_path(resource)
    end
end

# users/passwords_controller.rb
class Users::PasswordsController < Devise::PasswordsController
  protected
    def after_resetting_password_path_for(resource)
      signed_in_root_path(resource)
    end
end
于 2014-07-01T01:47:59.673 回答