我遇到了一个奇怪的情况发生在我能够通过Omniauth从本地运行时通过Omniauth读取并保存信息的地方,但是当我将确切代码推送到Heroku时,下面的错误(来自我的日志)关于列first_name。OmniAuth ActiveRecord错误推向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的个人资料,所以不要指望把所有的额外信息(例如关系状态等)。
我试图建立一个基本的新用户配置文件(在我的术语中称为“aquarists”),它将提取一些他们很乐意从facebook分享的额外信息。
当我在本地执行此操作时,它可以正常工作,我可以收集我的first_name,last_name,gender和locale作为实例并将其保存到数据库。
下面是我使用写
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上运行过我的迁移,并且已经像其他Q &一样建议 - 还使用'heroku restart'来确保数据库列在应用程序中被选中。
这是我的Gemfile我OmniAuth项:
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
我运行的Rails 3.1.3和Postgres 9.1.2本地。我使用我认为运行PG 8.x的免费Heroku数据库。
下面是创建特定的列迁移文件的摘录:
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版本问题吗?
你看到了什么,如果你检查通过控制台水族模型。它有这个专栏吗? – 2012-02-15 12:42:06
可惜我不能看到实际的PG数据库,因为我在Heroku上的免费版本。这就是说我的迁移上下工作正常,没有错误,所以我相信这些列正在创建。 – xander 2012-02-16 08:59:58
通过控制台看看。 heroku运行控制台; Aquarist.new – 2012-02-16 09:24:39