轨道4.1.5全能强参数 [英] Rails 4.1.5 omniauth strong parameters

查看:114
本文介绍了轨道4.1.5全能强参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Rails 4.1.4升级到4.1.5后,我的Facebook omniauth会话错误,从那以后,一切都正常。
当我创建一个用户会话我得到一个 ActiveModel :: ForbiddenAttributesError



路由:

  match'auth /:provider / callback',to:'sessions#create',as:'signin',via::get 

会话#创建控制器:

  def create 
user = User.from_omniauth(env [omniauth.auth])
会话[:user_id] = user.id
会话[:user_name] = user.name

redirect_to root_path
end

和a用户模型如下:

  def self.from_omniauth(auth)
其中(auth.slice(:provider, uid))。first_or_create.tap do | user |
user.provider || = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.save
end
end

我可以通过添加许可证来绕过ActiveModel错误!我的用户模型中的方法如下:

 其中(auth.slice(:provider,:uid).permit!)。first_or_create .tap do | user | 

但它覆盖数据库中的第一个用户...
session [:user_id] 似乎永远是数据库中的第一个用户。



我不知道这是一个很强的参数问题,Omniauth问题还是两者?

解决方案

替换您当前的查找器:



$ code> def self.from_omniauth(auth)
其中(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


After upgrading Rails 4.1.4 to 4.1.5 i get errors with my facebook omniauth session everything was working fine since then. When i create a User Session i get an ActiveModel::ForbiddenAttributesError

Route:

  match 'auth/:provider/callback', to: 'sessions#create', as: 'signin', via: :get

Session#create controller:

  def create
        user = User.from_omniauth(env["omniauth.auth"])
        session[:user_id] = user.id 
        session[:user_name] = user.name

      redirect_to root_path
  end

and a user model like this:

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

I can bypass the ActiveModel error by adding a permit! method in my User Model like that:

where(auth.slice(:provider, :uid).permit!).first_or_create.tap do |user|

But it override the first user from the database... The session[:user_id] seems to always be the first User from the database.

I don't know if it's a strong parameters problem, an Omniauth problem or both?

解决方案

Replace you current finder:

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

这篇关于轨道4.1.5全能强参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆