rails 上的 ForbiddenAttributesError ruby
ForbiddenAttributesError ruby on rails
我在 Ruby 中有这个模型,但它抛出了一个 ActiveModel::ForbiddenAttributesError
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
于 ruby 2.2.1p85(2015-02-26 修订版 49769)[x86_64-linux]
你能告诉我如何摆脱这个错误吗?
提前致谢。
我认为 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 对我有帮助。
我在 Ruby 中有这个模型,但它抛出了一个 ActiveModel::ForbiddenAttributesError
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
于 ruby 2.2.1p85(2015-02-26 修订版 49769)[x86_64-linux]
你能告诉我如何摆脱这个错误吗?
提前致谢。
我认为 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 对我有帮助。