AuthenticationsController中的NoMethodError #create | omn​​iauth + devise

时间:2011-05-15 20:16:52

标签: ruby-on-rails-3 devise omniauth

AuthenticationsController中的NoMethodError #create

未定义方法`user'for#/ Authentication:0x00000105c7b1f8 \

我把头发拉了出来..我从railscasts下载了示例应用程序并且它可以工作..我看起来像是最后一个bug。

我正在尝试按照railscast将其添加到我的应用程序中,否则它正在工作 - 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'}

1 个答案:

答案 0 :(得分:0)

sign_in_and_redirect(:user,user)中应该有@user。不是user