开发者

NoMethodError in AuthenticationsController#create | omniauth + devise

NoMethodError in AuthenticationsController#create

undefined method `user' for # /Authentication:0x00000105c7b1f8\

I'm pulling my hair out.. I downloaded the sample app from railscasts and it works.. mine has what looks like one last bug.

I am attempting to follow the railscasts to add this in to my app that is/was otherwise working - http://railscasts.com/episodes/236-omniauth-part-2

It seems that it's mostly getting there - in the authorizations table there is a record..

I'm not getting as far as the step where the page asks for an email because it doesn't have one.

I'm sure this is s开发者_如何学Gouper simple - here are some snippets:

controller:

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

devise user model:

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

some of the routes.rb:

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

  get "search/show"

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


There should be @user in sign_in_and_redirect(:user, user). Not user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜