加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 什么是在rubocop中隐藏外部局部变量,我该如何

发布时间:2020-12-17 03:32:53 所属栏目:百科 来源:网络整理
导读:我在rails上运行rubocop,它给了我下面的消息. W: Shadowing outer local variable - user. where(provider: auth.provider,uid: auth.uid).first_or_create do |user| ^^^^ 这是代码. def self.from_omniauth(auth)user = User.where(email: auth.info.email
我在rails上运行rubocop,它给了我下面的消息.

W: Shadowing outer local variable - user.
  where(provider: auth.provider,uid: auth.uid).first_or_create do |user|
                                                                    ^^^^

这是代码.

def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first

if user
  return user
else
  where(provider: auth.provider,uid: auth.uid).first_or_create do |user|
    user.fullname = auth.info.name
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
    user.image = auth.info.image
    user.password = Devise.friendly_token[0,20]
  end
end

结束

解决方法

这意味着作为块参数提供的用户将覆盖此处定义的用户变量user = User.where(email:auth.info.email).first

要克服它,您需要更改其中一个变量的名称.
要么是谎言:

result = User.where...
return result if result

要么:

where(provider: auth.provider,uid: auth.uid).first_or_create do |u|
  u.fullname = auth.info.name
  ...
end

更多信息:https://github.com/bbatsov/ruby-style-guide#no-shadowing

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读