2011-09-23 47 views
0

我一直在这一个拉我的头发几个小时...我有一个Devise和omniauth设置,允许用户使用他们的Facebook帐户登录。我使用下面的宝石版本:Heroku的Devise + omniauth路由问题

Installing devise (1.4.7) 
Installing oauth2 (0.4.1) 
Installing oa-oauth (0.2.6) 
Installing omniauth (0.2.6) 

等等,并在我的设计初始化应用程序ID和秘密:

config.omniauth :facebook, 'app_id', 'app_secret', 
      {:scope => 'email, offline_access', :client_options => {:ssl => {:verify => false}}} 

虽然我也试过这个没有运气:

config.omniauth :facebook, 'app_id', 'app_secret', 
      # {:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}} 

我在我的用户模型中有:omniauthable指令,并且我在路由文件中定义了我自己的回调控制器:

devise_for :users, :controllers => { :omniauth_callbacks => 'user/authentications', :registrations => 'registrations' } 

root :to => 'pages#home' 

而且我也尝试添加这对路由文件,没有运气:

get '/users/auth/:provider' => 'user/authentications#passthru' 

这里是我的认证控制器:

class AuthenticationsController < Devise::OmniauthCallbacksController 
    def index 
    @authentications = current_user.authentications if current_user 
    end 

    def facebook 
    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 

    def passthru 
    render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false 
    end 

    def destroy 
    @authentication = current_user.authentications.find(params[:id]) 
    @authentication.destroy 
    flash[:notice] = "Successfully destroyed authentication." 
    redirect_to authentications_url 
    end 
end 

我的Facebook帐号登录链接:

< %= link_to image_tag('/ assets/facebook_32.png'),omniauth_authorize_path(resource_name,:facebook)%>

这一切的伟大工程在当地,但如果我尝试当我的应用程序部署到Heroku上与我的Facebook帐户登录,我得到了我的日志下面,我引导到404页:

2011-09-23T03:26:31+00:00 app[web.1]: ActionController::RoutingError (uninitiali 
zed constant User::AuthenticationsController): 

我试着重置我的数据库,但这也没有帮助。

这里是我的路线从Heroku的:

 new_user_session GET /users/sign_in(.:format)    {:action= 
>"new", :controller=>"devise/sessions"} 
      user_session POST /users/sign_in(.:format)    {:action= 
>"create", :controller=>"devise/sessions"} 
    destroy_user_session GET /users/sign_out(.:format)    {:action= 
>"destroy", :controller=>"devise/sessions"} 
    user_omniauth_callback  /users/auth/:action/callback(.:format) {:action= 
>/facebook/, :controller=>"user/authentications"} 
      user_password POST /users/password(.:format)    {:action= 
>"create", :controller=>"devise/passwords"} 
     new_user_password GET /users/password/new(.:format)   {:action= 
>"new", :controller=>"devise/passwords"} 
     edit_user_password GET /users/password/edit(.:format)   {:action= 
>"edit", :controller=>"devise/passwords"} 
         PUT /users/password(.:format)    {:action= 
>"update", :controller=>"devise/passwords"} 
cancel_user_registration GET /users/cancel(.:format)    {:action= 
>"cancel", :controller=>"registrations"} 
     user_registration POST /users(.:format)      {:action= 
>"create", :controller=>"registrations"} 
    new_user_registration GET /users/sign_up(.:format)    {:action= 
>"new", :controller=>"registrations"} 
    edit_user_registration GET /users/edit(.:format)     {:action= 
>"edit", :controller=>"registrations"} 
         PUT /users(.:format)      {:action= 
>"update", :controller=>"registrations"} 
         DELETE /users(.:format)      {:action= 
>"destroy", :controller=>"registrations"} 
        root  /         {:control 
ler=>"pages", :action=>"home"} 

任何帮助是极大的赞赏。如果您需要其他信息,请告诉我。我知道这很小,但我觉得我已经尝试了一千种不同的东西,没有任何工作。

+0

是不是您的“passthru”行动呈现404.html。如果你在做'/ users/auth /:provider'=>'user/authentications#passthru' –

+0

这是,但我目前没有使用它。 –

回答

0

我认为,你的authenicatine行应该使用find_or_create。

 
authentication = Authentication.find_or_create_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 
+0

我正在查看验证后是否为零。如果它是零,它会在我的控制器中进一步向下创建。 –

0

我最终改变

devise_for :users, :controllers => { :omniauth_callbacks => 'user/authentications', :registrations => 'registrations' } 

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

,并开始在本地和在Heroku工作。