【发布时间】:2018-02-07 13:29:43
【问题描述】:
我开始观看 Ryan Bates 的 tutorial 关于如何将 Omniauth 与设计一起使用(rails-cast 235 已修订)。我遇到了 user.rb 文件的问题。他在tutorial 中显示的内容
devise :omniauthable, # ...
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.username = auth.info.nickname
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"], without_protection: true) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
对我不起作用,它显示一个 ActiveModel::ForbiddenAttributesError 并突出显示这行代码where(auth.slice(:provider, :uid)).first_or_create do |user|。我认为他的版本不起作用,因为我使用的是 Rails 5。无论如何我已经尝试修改 user.rb 文件。使用以下代码。
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.provider = auth.provider
user.uid = auth.uid
token = auth.credentials.token,
secret = auth.credentials.secret
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.twitter_data"]
# user.attributes = params
user.update(
email: params[:email],
password: Devise.friendly_token[0,20],
provider: data["provider"],
uid: data["token"],
token: data["credentials"]["token"],
secret: data["credentials"]["secret"]
)
end
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
end
我能够让 twitter 身份验证页面显示加载大约一分钟,然后在不登录且不显示任何错误消息的情况下重定向回我的用户注册页面。顺便说一句,有人知道如何使用 devise 登录 facebook。
【问题讨论】:
-
不要按照 Ryan Bates 配置omniauth,只需按照以下github存储库中的步骤进行操作即可github.com/plataformatec/devisegithub.com/omniauth/omniauthrubydoc.info/github/plataformatec/devise/master/Devise/Models/…digitalocean.com/community/tutorials/…
标签: ruby-on-rails twitter devise ruby-on-rails-5 omniauth-twitter