rake routes表示路由存在但错误消息另有说明

时间:2013-02-20 21:46:38

标签: ruby-on-rails

我正在跟随Railscast http://railscasts.com/episodes/235-devise-and-omniauth-revised?view=comments关于将omniauth实现到devise中,特别是添加了Twitter signin。当我添加链接以登录Twitter到我的应用程序时,我收到此错误访问主页

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

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

如果我做rake路由(见下文),它表明我有'user_omniauth_callback'路由,但它没有指定请求类型(GET,POST)等,我不确定是否重要。

我在SO上搜索了这个错误,并且有一个人因为在routes.rb中不包含以下代码而导致了类似的问题,但我已将其包括在内。

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

错误出现在他加入omniauth控制器的剧集部分之前;我添加它只是为了确定,但我仍然得到同样的错误:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

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

    end

    alias_method :twitter, :all
end

我不确定我还能看到什么。在设计初始化程序中,我为Twitter设置了配置

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

这是我的用户模型

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

您能就我可能采取的措施提出任何建议来解决这个问题吗?

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

1 个答案:

答案 0 :(得分:0)

错误在于我传递的是:提供者而不是:twitter作为链接中的参数。

不正确的

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

正确:

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