【问题标题】:Rails ActiveModel::ForbiddenAttributesError Devise, OmniauthRails ActiveModel::ForbiddenAttributesError 设计,Omniauth
【发布时间】: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


【解决方案1】:

几周前我遇到了同样的问题,并通过更改下面的行来解决它。本质上,您应该将参数显式传递到您的查询中。

where(provider: auth.provider, uid: auth.uid).first_or_create do |user|

你可以找到Devise issue关于这个。

【讨论】:

  • 感谢贾斯汀,这真的很有帮助。但是我还是不明白为什么这个问题在 localhost 开发中没有出现,而在 Heroku 中才开始出现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多