推送到 Heroku 时出现 OmniAuth ActiveRecord 错误
我遇到了一种奇怪的情况,在本地运行时,我能够通过 Omniauth 读取和保存来自 Facebook 响应的信息,但是当我将确切的代码推送到 Heroku 时,会出现以下错误(来自我的日志),内容涉及列名字。
Processing by AdminController#index as HTML
Rendered admin/list_users.html.erb within layouts/application (60.9ms)
Completed 500 Internal Server Error in 163ms
ActionView::Template::Error (undefined method `first_name' for #<Aquarist:0x00000001ee8>):
: <td>Nickname: <%= aquarist.nickname %>
: 32: Last: <%= aquarist.last_name %></td>
: 29: <td><%= aquarist.name %></td>
: 31: First: <%= aquarist.first_name %>
: 34: <td><%= aquarist.email %></td>
: 28: <td><%= image_tag(aquarist.image, :width => '20') %></td>
: 33: <td><%= aquarist.provider %></td>
我使用术语“水族爱好者”代替用户......我知道这不是标准用法,但对于我的用例来说,它似乎更有意义。我可能会及时将其更改回来...
这是我来自omniauth的Facebook回调:
--- !ruby/hash:OmniAuth::AuthHash
provider: facebook
uid: '12456789'
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
email: [email protected]
name: Alex
first_name: Alex
last_name: Lastname
image: http://graph.facebook.com/123456789/picture?type=square
urls: !ruby/hash:Hashie::Mash
Facebook: http://www.facebook.com/profile.php?id=12456789
credentials: !ruby/hash:Hashie::Mash
token: AACCCCb1i3ZALXEMfGxJtKZA
expires_at: 13378794564
expires: true
extra: !ruby/hash:Hashie::Mash
raw_info: !ruby/hash:Hashie::Mash
id: '12456789'
name: Alex Lastname
first_name: Alex
last_name: Lastname
link: http://www.facebook.com/profile.php?id=12456789
gender: male
email: [email protected]
timezone: 11
locale: en_US
verified: true
updated_time: '2012-02-01T12:51:00+0000'
如您所见,我已经锁定了我的Facebook个人资料,因此不要指望获得所有额外信息(例如关系状态等)。
我正在尝试建立新用户(我的术语中的“水族爱好者”)的基本档案,该档案将获取他们乐于从 Facebook 分享的一些额外信息。
当我在本地执行此操作时,效果很好,我可以收集我的名字、姓氏、性别和区域设置,并将其保存到数据库中。
这是我用来编写配置文件的代码
def self.create_with_omniauth(auth)
create! do |aquarist|
aquarist.provider = auth["provider"]
aquarist.uid = auth["uid"]
aquarist.name = auth["info"]["name"]
aquarist.nickname = auth["info"]["nickname"]
aquarist.email = auth["info"]["email"]
aquarist.image = auth["info"]["image"]
aquarist.first_name = auth["extra"]["raw_info"]["first_name"]
aquarist.last_name = auth["extra"]["raw_info"]["last_name"]
aquarist.user_location = auth["extra"]["raw_info"]["user_location"]
aquarist.user_hometown = auth["extra"]["raw_info"]["user_hometown"]
aquarist.age = auth["extra"]["raw_info"]["age"]
aquarist.locale = auth["extra"]["raw_info"]["locale"]
aquarist.gender = auth["extra"]["raw_info"]["gender"]
end
end
,然后我使用此代码来显示此配置文件信息(在表格中):
<% @aquarists.each do |aquarist|%>
<tr class="tbody">
<td><%= aquarist.uid %></td>
<td><%= image_tag(aquarist.image, :width => '20') %></td>
<td><%= aquarist.first_name %></td>
..and so on
当我推送此代码时,相同的信息会出现如上所述的活动记录错误。
但是,如果我从 [raw_info][extra] 部分中删除任何列,则代码可以在 Heroku 上运行(例如,全名、UID、提供程序等都保存到数据库中)。
让我完全困惑的是这段代码在本地工作 - 所以我认为我正在正确地从“raw_info”部分请求数据。
我已经确认我已经在 Heroku 上运行了迁移,并且正如其他一些问答所建议的那样,还使用了“heroku restart”来确保在应用程序中拾取数据库列。
这是我的 gemfile 中的 OmniAuth 条目:
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
我在本地运行 Rails 3.1.3 和 postgres 9.1.2。我正在使用免费的 Heroku 数据库,我相信它运行 PG 8.x。
以下是创建特定列的迁移文件的摘录:
def change
add_column("aquarists", "first_name", :text, :default => "")
add_column("aquarists", "last_name", :text, :default => "")
add_column("aquarists", "gender", :text, :default => "")
add_column("aquarists", "user_location", :text, :default => "")
add_column("aquarists", "user_relationships", :text, :default => "")
add_column("aquarists", "user_hometown", :text, :default => "")
add_column("aquarists", "age", :integer)
add_column("aquarists", "locale", :text, :default => "")
add_column("aquarists", "image", :text, :default => "")
add_column("aquarists", "timezone", :text, :default => "")
add_column("aquarists", "last_login", :datetime)
end
这是我运行 Heroku 控制台时返回的结果:
$ heroku run console
Running console attached to terminal... up, run.1
Loading production environment (Rails 3.1.3)
irb(main):001:0> Aquarist.new
=> #<Aquarist id: nil, nickname: nil, name: nil, email: nil, created_at: nil, updated_at: nil, provider: nil, uid: nil, admin: false, age: nil, locale: "", image: "", timezone: "", last_login: nil, visits: nil>
irb(main):002:0>
对我的代码命中 Heroku 时可能发生的情况有什么看法吗?是Postgres版本问题吗?
I'm having a strange situation occuring where I'm able to read and save information from a Facebook response via Omniauth when running locally, but when I push the exact code to Heroku the error below is coming up (from my logs) regarding the column first_name.
Processing by AdminController#index as HTML
Rendered admin/list_users.html.erb within layouts/application (60.9ms)
Completed 500 Internal Server Error in 163ms
ActionView::Template::Error (undefined method `first_name' for #<Aquarist:0x00000001ee8>):
: <td>Nickname: <%= aquarist.nickname %>
: 32: Last: <%= aquarist.last_name %></td>
: 29: <td><%= aquarist.name %></td>
: 31: First: <%= aquarist.first_name %>
: 34: <td><%= aquarist.email %></td>
: 28: <td><%= image_tag(aquarist.image, :width => '20') %></td>
: 33: <td><%= aquarist.provider %></td>
I'm using the term "aquarist" in place of user... I know this isn't standard use but for my use case it seems to make a bit more sense. I may change it back in time...
Here's my facebook callback from omniauth:
--- !ruby/hash:OmniAuth::AuthHash
provider: facebook
uid: '12456789'
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
email: [email protected]
name: Alex
first_name: Alex
last_name: Lastname
image: http://graph.facebook.com/123456789/picture?type=square
urls: !ruby/hash:Hashie::Mash
Facebook: http://www.facebook.com/profile.php?id=12456789
credentials: !ruby/hash:Hashie::Mash
token: AACCCCb1i3ZALXEMfGxJtKZA
expires_at: 13378794564
expires: true
extra: !ruby/hash:Hashie::Mash
raw_info: !ruby/hash:Hashie::Mash
id: '12456789'
name: Alex Lastname
first_name: Alex
last_name: Lastname
link: http://www.facebook.com/profile.php?id=12456789
gender: male
email: [email protected]
timezone: 11
locale: en_US
verified: true
updated_time: '2012-02-01T12:51:00+0000'
As you can see I've locked down my facebook profile so don't expect to get all the extra information (eg relationship status etc).
I'm trying to build a basic profile of new users ("aquarists" in my terminology) which will pick up some of the extra information they are happy to share from facebook.
When I do this locally it works fine, I can collect my first_name, last_name, gender and locale for instance and save it to the database.
Here's the code I'm using to write the profile
def self.create_with_omniauth(auth)
create! do |aquarist|
aquarist.provider = auth["provider"]
aquarist.uid = auth["uid"]
aquarist.name = auth["info"]["name"]
aquarist.nickname = auth["info"]["nickname"]
aquarist.email = auth["info"]["email"]
aquarist.image = auth["info"]["image"]
aquarist.first_name = auth["extra"]["raw_info"]["first_name"]
aquarist.last_name = auth["extra"]["raw_info"]["last_name"]
aquarist.user_location = auth["extra"]["raw_info"]["user_location"]
aquarist.user_hometown = auth["extra"]["raw_info"]["user_hometown"]
aquarist.age = auth["extra"]["raw_info"]["age"]
aquarist.locale = auth["extra"]["raw_info"]["locale"]
aquarist.gender = auth["extra"]["raw_info"]["gender"]
end
end
I'm then using this code to display this profile information (in a table):
<% @aquarists.each do |aquarist|%>
<tr class="tbody">
<td><%= aquarist.uid %></td>
<td><%= image_tag(aquarist.image, :width => '20') %></td>
<td><%= aquarist.first_name %></td>
..and so on
The identical information when I push this code comes up with the active record error as per above.
If however I remove any of the columns from the [raw_info][extra] section the code works on Heroku (eg full name, UID, provider etc are all saved to the db).
The thing that has me completely confused is that this code is working locally - so I gather I'm requesting data from the "raw_info" section correctly.
I've already confirmed I've run my migrations on Heroku, and have - as some of other Q&As suggest - also used 'heroku restart' to ensure the database columns are being picked up in the app.
This is my OmniAuth entry in my gemfile:
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
I'm running Rails 3.1.3 and postgres 9.1.2 locally. I'm using the free Heroku database which I believe runs PG 8.x.
Here's an extract from the migration file that creates the particular columns:
def change
add_column("aquarists", "first_name", :text, :default => "")
add_column("aquarists", "last_name", :text, :default => "")
add_column("aquarists", "gender", :text, :default => "")
add_column("aquarists", "user_location", :text, :default => "")
add_column("aquarists", "user_relationships", :text, :default => "")
add_column("aquarists", "user_hometown", :text, :default => "")
add_column("aquarists", "age", :integer)
add_column("aquarists", "locale", :text, :default => "")
add_column("aquarists", "image", :text, :default => "")
add_column("aquarists", "timezone", :text, :default => "")
add_column("aquarists", "last_login", :datetime)
end
And this is what comes back when I run heroku console:
$ heroku run console
Running console attached to terminal... up, run.1
Loading production environment (Rails 3.1.3)
irb(main):001:0> Aquarist.new
=> #<Aquarist id: nil, nickname: nil, name: nil, email: nil, created_at: nil, updated_at: nil, provider: nil, uid: nil, admin: false, age: nil, locale: "", image: "", timezone: "", last_login: nil, visits: nil>
irb(main):002:0>
Any views on what may be happening when my code is hitting Heroku? Is it a Postgres version issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚那是什么......一个拙劣的迁移文件。在此过程中,我对原始迁移文件进行了(愚蠢的)修改,并在我的开发环境中正确运行它,但忘记在线撤消更改。
不会再这样做了,我可以用生命中的 8 个小时左右的时间来做。 :)
Worked out what it was ... a botched migration file. Somewhere along the line I made a (silly) modification to an original migration file, and ran it correctly in my development environment but forgot to reverse the change online.
Won't do that again, I could do with the 8 or so hours back in my life. :)