0

我在 Omniauth 上关注本教程:http: //railscasts.com/episodes/235-omniauth-part-1?view=asciicast

我不断收到此错误:

没有这样的列:authentication.provider:

现在我想知道的主要事情是为什么“提供者”不被接受。它存在于类中……身份验证数据库存在……那为什么说它不存在呢?

这是我的身份验证控制器:

class AuthenticationsController < InheritedResources::Base
    def index
        @authentications = current_user.authentications if current_user
    end

    def create
        @user = User.where(authentication: auth).first_or_create(:provider => auth['provider'], :uid => auth['uid'])
        self.current_user = @user
        # auth = request.env["omniauth.auth"] current_user.authentications.create(:provider => auth['provider'], :uid => auth['uid'])
        flash[:notice] = "Authentication successful."
        redirect_to authentications_url
    end

    def auth
        request.env['omniauth.auth']
    end

    def destroy
        @authentication = current_user.authentications.find(params[:id])
        @authentication.destroy
        flash[:notice] = "Successfully destroyed authentication."
        redirect_to authentications_url
    end
end

我可以向您保证,我有一个称为身份验证的模型,并且该模型具有提供程序和 uid 字段。我也试过 where(authentications: auth) 和 where(auth: auth) 都没有运气。

任何想法,将不胜感激。

更新

authentication.rb (模型)

class Authentication < ActiveRecord::Base
attr_accessible :create, :destroy, :index, :provider, :uid, :user_id
belongs_to :user
end

更新 2

我基本上是在尝试使本教程适应 rails 3.2。

教程中的原始行已在上面注释掉。

更新 3 这是整个第一行错误:

SQLite3::SQLException: 没有这样的列: authentication.provider: SELECT "users".* FROM "users" WHERE "authentication"."provider" = 'facebook' AND "authentication"."uid" = '2222222' AND "authentication "."info" = '--- !ruby/hash:OmniAuth::AuthHash::InfoHash

讨厌成为负担......但时间真的很紧迫,我的屁股已经上线了,我要完全疯了,试图弄清楚这一点。如果您能告诉我为什么不接受提供者,我相信我可以弄清楚其余的。

4

3 回答 3

0

由于您只是在authentications表中添加一条记录,我无法理解您为什么要重新分配this.current_user. 也是current_user辅助方法或成员,如果它是在哪里声明的成员?

您不只是想为当前用户创建身份验证吗?:

def create
    current_user.authentications.first_or_create(:provider => auth['provider'], :uid => auth['uid'])
    flash[:notice] = "Authentication successful."
    redirect_to authentications_url
end

这将通过提供者和 uid 查找第一个身份验证记录,如果未找到,则创建该身份验证记录。

同样由于那个错误,我希望你已经找到了这个问题的答案:

现在我想知道的主要事情是为什么“提供者”不被接受。它存在于类中……身份验证数据库存在……那为什么说它不存在呢?

这是因为您正在调用first_or_create()object User,而不是Authentication.

于 2013-07-02T13:35:28.327 回答
0

你的创建动作没有意义

User.where(authentication: auth)转换为SELECT * FROM users WHERE authentication = a_hash

你应该做类似的事情

auth1 = Authentication.where(provider: auth['provider'], uid: auth['uid']).first
if !auth1.nil?
  current_user = auth.user
else
  user = User.new
  user.authentications.build(provider: auth['provider'], uid: auth['uid'])
  user.save!
  current_user = user
end
于 2013-07-02T03:15:21.687 回答
0

我最近也遇到了这个问题。起初我以为我忘记在 users 表中添加提供者列,但事实并非如此。这就是我最终解决它的方法:

def self.from_omniauth(auth)
  where(provider: auth["provider"], uid: auth["uid"]).first_or_create do |user|
    user.email = auth["info"]["email"]
    user.password = Devise.friendly_token[0, 20]
    user.logo = auth["info"]["image"]
    # if you use confirmable, since facebook validates emails
    # skip confirmation emails
    user.skip_confirmation!
  end
end

auth 是一个类似于下面的哈希,所以我使用 auth["provider"] 等代替 auth.provider:

omniauth.auth: {"provider"=>"facebook", "uid"=>"11111111111111", "info"=>{"email"=>"some@email.com", "image"=>"http://graph.facebook.com/v2.6/11111111111111/picture"}, "credentials"=>{"token"=>"sometoken", "expires_at"=>1506680013, "expires"=>true}, "extra"=>{"raw_info"=>{"email"=>"some@email.com", "id"=>"11111111111111"}}}
于 2017-08-01T09:07:03.133 回答