2011-05-15 98 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

相关问题