控制器不保存属性(来自FB的Oauth Token)

时间:2013-04-18 20:17:49

标签: ruby-on-rails omniauth

奇怪的问题 - 我的一个控制器中的创建操作没有保存属性 - 请查看下面的代码。所有行都工作到最后2行。oauth_tokenoauth_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

2 个答案:

答案 0 :(得分: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,你可以使用或(||)运算符对除了最后一个之外的所有操作符进行更简化:

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_tokenoauth_expires_at执行的任何操作导致问题。

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

答案 1 :(得分: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)