简体   繁体   中英

rake routes says route exists but error message says otherwise

I'm following along with a Railscast http://railscasts.com/episodes/235-devise-and-omniauth-revised?view=comments about implementing omniauth into devise, specifically adding Twitter signin. When I add the link to sign into Twitter to my app, I'm getting this error visiting the homepage

  <li><%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:provider) %></li>

No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:provider}

If I do rake routes (see below), it shows that I have 'user_omniauth_callback' route, but it doesn't specify a request type (GET, POST) etc, which I'm not sure is significant or not.

I searched this error on SO, and one person had a similar problem that resulted from not including the following code in routes.rb, but I have it included.

devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
                   controllers: {omniauth_callbacks: "omniauth_callbacks"}

The error arose before getting to the part of the episode where he adds the omniauth controller; I added it just to be sure but I'm still getting the same error:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

    def all
        request.env["omniauth.auth"]

    end

    alias_method :twitter, :all
end

I'm not really sure what else I can look at. In the devise initializer, I set up config for Twitter

config.omniauth :twitter, ENV["TWITTER_KEY"], ENV["TWITTER_SECRET"]

This is my user model

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :omniauthable,
         :recoverable, :rememberable, :trackable, :validatable


  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :image, :provider, :uid

  validates_uniqueness_of :name, :email, :case_sensitive => false
end

Can you make any suggestions about what I might do to fix this problem?

Rake Routes

 new_user_session GET    /users/sign_in(.:format)               devise/sessions#new
            user_session POST   /users/sign_in(.:format)               devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)              devise/sessions#destroy
 user_omniauth_authorize        /users/auth/:provider(.:format)        devise/omniauth_callbacks#passthru {:provider=>/twitter/}
  user_omniauth_callback        /users/auth/:action/callback(.:format) devise/omniauth_callbacks#(?-mix:twitter)
           user_password POST   /users/password(.:format)              devise/passwords#create
       new_user_password GET    /users/password/new(.:format)          devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)         devise/passwords#edit
                         PUT    /users/password(.:format)              devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                devise/registrations#cancel
       user_registration POST   /users(.:format)                       devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)               devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                  devise/registrations#edit
                         PUT    /users(.:format)                       devise/registrations#update
                         DELETE /users(.:format)                       devise/registrations#destroy

The mistake was that I was passing :provider rather than :twitter as an argument in the link.

Incorrect

<%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:provider) %>

Correct:

 <%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:twitter) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM