奇怪的问题 - 我的一个控制器中的创建操作没有保存属性 - 请查看下面的代码。所有行都工作到最后2行。oauth_token
和oauth_expires_at
未保存。我已经检查过属性是否正确命名等等。有趣的是,如果我将第二行更改为最后一行:
omniauth['credentials']['token'] ? provider = omniauth['credentials']['token'] : provider = ''
它完美无缺。实际属性oauth_token
和oauth_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
答案 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_token
和oauth_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)