0

奇怪的问题——我的一个控制器中的创建操作没有保存属性——查看下面的代码。所有的行都工作到最后 2 行。 theoauth_token和 theoauth_expires_at没有被保存。我检查了属性是否正确命名,等等。有趣的是,如果我将倒数第二行更改为:

omniauth['credentials']['token'] ? provider = omniauth['credentials']['token'] : provider = ''

它工作得很好。实际属性oauth_tokenoauth_expires_at. 然后我尝试创建 2 个新的虚拟属性以查看它是否可以在那里工作 - 不能。似乎之后创建的任何属性oauth_token都搞砸了。这是迁移问题吗?有任何想法吗?

def create
    params[:authorization] ? authorization_route = params[:authorization] : authorization_route = 'no authorization (invalid callback)'

    omniauth = request.env["omniauth.auth"]
    if omniauth and params[:authorization]
        if authorization_route == "facebook"
              omniauth['extra']['raw_info']['email'] ? email =  omniauth['extra']['raw_info']['email'] : email = ''
              omniauth['extra']['raw_info']['name'] ? name =  omniauth['extra']['raw_info']['name'] : name = ''
              omniauth['extra']['raw_info']['id'] ?  uid =  omniauth['extra']['raw_info']['id'] : uid = ''
              omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
              omniauth['credentials']['token'] ? oauth_token =  omniauth['credentials']['token'] : oauth_token = ''
              omniauth['credentials']['expires_at'] ? oauth_expires_at =  Time.at(omniauth['credentials']['expires_at']) : oauth_expires_at = ''
end
   end
4

2 回答 2

1

我不确定这是否是您的问题,但我使用三元 ( condition ? value : other_value) 语法的方式是非典型的。通常,你会写类似

def create
  authorization_route = params[:authorization] ? params[:authorization] : 'no authorization (invalid callback)'

  omniauth = request.env["omniauth.auth"]
  if omniauth and params[:authorization]
    if authorization_route == "facebook"
      email = omniauth['extra']['raw_info']['email'] ? omniauth['extra']['raw_info']['email'] : ''
      name = omniauth['extra']['raw_info']['name'] ? omniauth['extra']['raw_info']['name'] : ''
      uid = omniauth['extra']['raw_info']['id'] ? omniauth['extra']['raw_info']['id'] : ''
      provider = omniauth['provider'] ? omniauth['provider'] : ''
      oauth_token = omniauth['credentials']['token'] ? omniauth['credentials']['token'] : ''
      oauth_expires_at = omniauth['credentials']['expires_at'] ? Time.at(omniauth['credentials']['expires_at']) : ''
    end
  end
  # ...
end

但是,由于您使用它来执行此操作,因此您可以通过对除最后一个以外的所有操作attribute = value ? value : default使用 or () 操作符来进一步简化它:||

def create
  authorization_route = params[:authorization] || 'no authorization (invalid callback)'

  omniauth = request.env["omniauth.auth"]
  if omniauth and params[:authorization]
    if authorization_route == "facebook"
      email = omniauth['extra']['raw_info']['email'] || ''
      name = omniauth['extra']['raw_info']['name'] || ''
      uid = omniauth['extra']['raw_info']['id'] || ''
      provider = omniauth['provider'] || ''
      oauth_token = omniauth['credentials']['token'] || ''
      oauth_expires_at = omniauth['credentials']['expires_at'] ? Time.at(omniauth['credentials']['expires_at']) : ''
    end
  end
  # ...
end

但是,所有这三种方法都可以工作,因此要么omniauth['credentials']['token']不是正确的哈希键,要么你正在使用的任何东西oauth_token以及oauth_expires_at稍后在控制器或模型中进行的操作都会导致问题。

您可以编辑您的问题以包含来自控制器和用户模型的更多代码吗?

于 2013-04-18T20:28:10.897 回答
0

我很尴尬地承认问题实际上是什么:

完全忘记了我后来在控制器中所做的事情。一定要爱粗心的错误...

不得不改变:

user.authorizations.build(:provider => provider, :uid => uid, :name => name, :email => email, :user_id => user.id)

到:

user.authorizations.build(:provider => provider, :uid => uid, :name => name, :email => email, :user_id => user.id, :oauth_token => oauth_token, :oauth_expires_at => oauth_expires_at)
于 2013-04-18T20:34:40.970 回答