ローカルで実行しているときにOmniauthを介してFacebookの応答から情報を読み取って保存できるという奇妙な状況が発生していますが、正確なコードをHerokuにプッシュすると、以下のエラーが(ログから)表示されます。列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>
私はユーザーの代わりに「アクアリスト」という用語を使用しています...これは標準的な使用法ではないことは知っていますが、私の使用例ではもう少し理にかなっているようです。時間を遡って変更する場合があります...
これがomniauthからの私のFacebookコールバックです:
--- !ruby/hash:OmniAuth::AuthHash
provider: facebook
uid: '12456789'
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
email: my@email.address.com
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: my@email.address.com
timezone: 11
locale: en_US
verified: true
updated_time: '2012-02-01T12:51:00+0000'
ご覧のとおり、Facebookプロファイルをロックダウンしているので、すべての追加情報(たとえば、関係のステータスなど)を取得することを期待しないでください。
私は、新しいユーザー(私の用語では「アクアリスト」)の基本的なプロファイルを作成しようとしています。これにより、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&Aで示唆されているように、データベース列がアプリで確実に取得されるように「herokurestart」も使用しています。
これは私のgemfileのOmniAuthエントリです:
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
Rails3.1.3とpostgres9.1.2をローカルで実行しています。PG8.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バージョンの問題ですか?