Devise Omniauth undefined method omniauth_authorize_path

巧了我就是萌 提交于 2019-12-08 16:18:07

问题


I've noticed that when logging to Devise I have started to receive these error message.

I'm using Devise 2.2.4 with Omniauth 1.1.4 and Omniauth-Facebook 1.4.1

Does anybody know what is the cause of this error?

  ActionView::Template::Error (undefined method `omniauth_authorize_path' for #<#<Class:0xb85e534>:0xb904e5c>):
21: <%- if devise_mapping.omniauthable? %>
22:   <%- resource_class.omniauth_providers.each do |provider| %>
23:     <% logger.info "hey #{provider} , dolphin and #{resource_name}" %>
24:     <%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
25:   <% end -%>
26: <% end -%>

  app/views/devise/shared/_links.erb:24:in `block in _app_views_devise_shared__links_erb___1039642231_94147460'
  app/views/devise/shared/_links.erb:22:in `each'
  app/views/devise/shared/_links.erb:22:in `_app_views_devise_shared__links_erb___1039642231_94147460'
  app/views/devise/sessions/new.html.erb:17:in `_app_views_devise_sessions_new_html_erb__883448937_92868060'

回答1:


One possible mistake is that omniauth configuration is set at the wrong place.

I encountered this error because I set my facebook accounts in config/initializers/omniauth.rb, as instructed by the omniauth readme.

However we need to set it through devise, i.e. config/initializers/devise.rb at the omniauth section.




回答2:


I started getting this error today (Jul 27, 2016) when I upgraded to Ruby 2.3.1 and Rails 4.2.7. The solution that worked for me was to change all instances of user_omniauth_authorize_path(:twitter) to user_twitter_omniauth_authorize_path.




回答3:


Try

user_omniauth_authorize_path(provider)

I'm assuming you have a User class and in your routes file you have

devise_for :users



回答4:


Do it like that

<%- if devise_mapping.omniauthable? %>
  <%- resource_class.omniauth_providers.each do |provider| %>
    <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", public_send("user_#{provider.to_s}_omniauth_authorize_path") %><br />
  <% end -%>
<% end -%>

This makes it usable for multiple providers but it assumes you are using

devise_for :users

But going even further you could also add

resource_class.name.downcase

to cover not only user

<%- if devise_mapping.omniauthable? %>
  <%- resource_class.omniauth_providers.each do |provider| %>
     <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", public_send("#{resource_class.name.downcase}_#{provider.to_s}_omniauth_authorize_path") %><br />
  <% end -%>
<% end -%>

If devise_for is users and provider is facebook, then it will make path:

user_facebook_omniauth_authorize_path

if devise_for is admins and provider twitter, then it will make path:

admin_twitter_omniauth_authorize_path




回答5:


Devise changed the url helper to be omniauth_authorize_path(<scope>, <provider>)

See here: http://www.rubydoc.info/github/plataformatec/devise/Devise%2FOmniAuth%2FUrlHelpers%3Aomniauth_authorize_path




回答6:


In your app/views/devise/shared/_links.erb :

change

omniauth_authorize_path

to

user_omniauth_authorize_path(provider)




回答7:


If you initialize your devise providers in config/initializers/omniauth.rb You should include Devise::OmniAuth::UrlHelpers in your config/initializers/omniauth.rb or config/initializers/devise.rb



来源:https://stackoverflow.com/questions/19003958/devise-omniauth-undefined-method-omniauth-authorize-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!