【发布时间】:2015-02-07 20:07:11
【问题描述】:
我遇到了 Omniauth 身份验证和 Rails 4 的问题,因此我得到了 Rails ActiveModel::ForbiddenAttributesError。
我使用的是gem 'protected_attributes',所以强参数应该不是问题。
我的用户模型包含以下内容:
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.username = auth.info.email
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name
end
end
user.password 只是为了保持与现有 Devise 身份验证系统的兼容性。
AR 错误表明这一行:where(auth.slice(:provider, :uid)).first_or_create do |user| 正在引发错误。
上面的方法被调用:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def mavenlink
@user = User.from_omniauth(request.env['omniauth.auth'])
service = @user.services.initialize_or_update_via_omniauth(request.env['omniauth.auth'])
if service && service.save
sign_in_and_redirect @user #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Mavenlink") if is_navigational_format?
else
redirect_to root_path, error: "Error signing in with Mavenlink credentials."
end
end
end
这是否相关,我不确定,但我也一直在运行这个错误:
找不到路径“/auth/mavenlink/callback”的有效映射
也许不相关,但我想我会包括它以防万一。
任何帮助将不胜感激!
【问题讨论】:
-
将该行更改为
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|。这应该可以解决问题。 -
成功了,谢谢!为什么这是个问题?
标签: ruby-on-rails ruby devise omniauth strong-parameters