2014-04-09 120 views
1

我對RoR很陌生,對我很裸露。我已按照railscasts視頻#235和#236設置OmniAuth w/Devise。我可以讓一切正常工作,當我在驗證控制器中添加條件語句時,我的問題就出現了。我收到以下錯誤:Devise + Omniauth + Facebook

NoMethodError在AuthenticationsController#Facebook的 未定義的方法`TO_KEY」爲:用戶:符號 這似乎在被crapping出來:

sign_in_and_redirect(:user, authentication.user)

我在這裏檢查其他的答案,似乎沒有有一個解決方案。我發現一個與Heroku有關,但我正在開發這個消息。任何幫助將不勝感激。

authentications_controller.rb

 
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

registrations_controller.rb

 

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

     private 

     def build_resource(*args) 
     super 
     if session[:omniauth] 
      @user.apply_omniauth(session[:omniauth]) 
      @user.valid? 
     end 
     end 
    end 

的routes.rb

 

    resources :authentications 
     get '/auth/:provider/callback' => 'authentications#facebook' 
     devise_for :users, :controllers => {:registrations => 'registrations', :omniauth_callbacks => 'authentications'} 

user.rb

 

    class User ActiveRecord::Base 
     has_many :authentications 

     # Include default devise modules. Others available are: 
     # :confirmable, :lockable, :timeoutable and :omniauthable 
     devise :database_authenticatable, :registerable, 
      :recoverable, :rememberable, :trackable, :validatable, :omniauthable 

     def apply_omniauth(omniauth) 
     self.email = omniauth.info.email if email.blank? 
     authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid']) 
     end 

     def password_required? 
     (authentications.empty? || !password.blank?) && super 
     end 
    end 

登錄

 
ActiveRecord::SchemaMigration Load (0.9ms) SELECT "schema_migrations".* FROM "schema_migrations" 
I, [2014-04-09T20:17:07.296969 #2248] INFO -- omniauth: (facebook) Request phase initiated. 
I, [2014-04-09T20:17:07.353447 #2248] INFO -- omniauth: (facebook) Request phase initiated. 
I, [2014-04-09T20:17:07.518464 #2248] INFO -- omniauth: (facebook) Callback phase initiated. 
Processing by AuthenticationsController#facebook as HTML 
    Parameters: {"code"=>"AQC6I3SgbGI86ZBptFdBal5HIE1UHwCt7Zw5FVzGvWtApGhZfxjmXTo-AX3qJGOA_PWOeMK4i05K2yJL4_8bQLaTUjyvOBO4nQHjz2nLv5CTCMg2fPAsveUquO-UdVA8XajfUai9AQ8U8m4tfkwxysnq4n2bGBot2UCmFcfRGDB_b9uoIO8ELj80ltc3T0Vf2wj6XScl_R5m1o3CTUYqUzLQCukyjCb3Mz5_2_cWvHiCJLEiTMWjwccbrqNFhWgFCDNE0sLWZnESFNbdWUvFb_ug0NoXk4JevtQjv9XbW-FBiZ3FACn49grGlum1Z0I6qbI", "state"=>"758f521ee2e37251f404435d13e5f194fc7dab59cb412659"} 
    Authentication Load (2.2ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND "authentications"."uid" = '578621157' ORDER BY "authentications"."id" ASC LIMIT 1 
    User Load (7.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]] 
Completed 500 Internal Server Error in 124ms 
    Rendered /Users/csmears/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.3ms) 
    Rendered /Users/csmears/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.2ms) 
    Rendered /Users/csmears/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.9ms) 
    Rendered /Users/csmears/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (35.6ms) 

+0

我也檢查了設計和omniauth文檔,但似乎無法找到解決方案。 – user3517252

回答

0

變化

sign_in_and_redirect(:user, authentication.user) 

sign_in_and_redirect(authentication.user) 
+0

更改結果時出現新錯誤:AuthenticationsController中的RuntimeError#facebook 無法找到無效的有效映射 – user3517252

+0

您能共享爲此錯誤生成的服務器日誌嗎?在問題中發佈它。 –

1

的問題是與哈希格式。我改變了它:

sign_in_and_redirect(:user, authentication.user.user_id)

並解決了它。

相關問題