2016-02-09 57 views
0

我在Ruby的这种模式,但它抛出一个ActiveModel::ForbiddenAttributesErrorForbiddenAttributesError红宝石

class User < ActiveRecord::Base 
def self.from_omniauth(auth) 
    where(auth.slice("uid", "nickname", "image")).first || create_from_omniauth(auth) 
end 

def self.create_from_omniauth(auth) 
    create! do |user| 
     user.uid = auth["uid"] 
     user.name = auth["info"]["nickname"] 
     user.image = auth["info"]["image"] 
    end 
    end 
end 

当我运行这个动作

def auth_callback 
    user = User.from_omniauth(env["omniauth.auth"]) 
session[:user_id] = user.id 
redirect_to root_url, notice: "signed in!" 

end 
end 

上红宝石2.2.1p85(2015年2月26日修订49769 )[x86_64-linux]

你能告诉我如何摆脱这个错误?

在此先感谢。

+0

我不知道这[问题](https://github.com/plataformatec/devise/issues/3157)是否与此相关。 –

+0

我不认为这是同一个问题。 –

回答

1

我认为slice方法不再可用来绕过强参数。 试试这个代码,它试图以更具体的身份验证选项:

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
    user.provider = auth.provider 
    user.uid  = auth.uid 
    user.name  = auth.info.name 
    user.save 
    end 
end 

您可能可能需要使用auth[:provider]而不是auth.provider。 让我知道如果这对你有用的评论。 您也可以浏览this question,帮助我。

+0

这是问题所在,谢谢! –