2011-05-15 40 views
1

NoMethodError在AuthenticationsController#創建AuthenticationsController中的NoMethodError#create | omn​​iauth +設計

未定義的方法'用戶」的#/驗證:0x00000105c7b1f8 \

我拉我的頭髮。我從網上下載railscasts示例應用程序和它的作品。我的看起來像是最後一個bug。

我試圖按照railscasts在我的應用程序,是/是工作,不然添加此 - http://railscasts.com/episodes/236-omniauth-part-2

看來,這主要是到達那裏 - 在授權表中存在記錄的..

我沒有達到頁面要求輸入電子郵件的步驟,因爲它沒有電子郵件。

我敢肯定,這是超級簡單 - 這裏有一些片段:

控制器:

class AuthenticationsController < ApplicationController 

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

end 

色器件用戶模式:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, :lockable and :timeoutable 
    has_many :authentications 

    devise :database_authenticatable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    #attr_accessible :email, :password, :password_confirmation, :remember_me 

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

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

end 

一些routes.rb中的:

resources :authentications 
    match '/auth/:provider/callback' => 'authentications#create' 

    get "search/show" 

    #devise_for :users 
    devise_for :users, :controllers => {:registrations => 'registrations'} 

回答

0

那裏守ld在sign_in_and_redirect(:user,user)中爲@user。不是user

+0

這工作!我知道這很簡單..但不是那麼簡單:)謝謝你! (我有新問題,但我會通過這些工作) – 2011-05-17 04:49:23

+0

...或者相反,您應該在前三行使用'user',即'user = User.new; user.apply_omniauth(omniauth);如果user.save ...'實例變量與本地變量不同 - 你不能混合它們,除了一個真的不會,因爲它們暗示着不同的目的。 – 2011-06-20 09:35:58

相關問題