2012-03-08 9 views
1

simple_formと連携するのに問題があります。フォームでの関連付けができません。

私は以下のモデルを持っています。

class Product < ActiveRecord::Base 
    belongs_to :Retailer 
end 

class Retailer < ActiveRecord::Base 
    has_many :Products 
end 

マイ形部分(製品/ _form.html.erb)それは関連せずに動作しますが、それを私は次のように取得するには、次

<%= simple_form_for(@product) do |f| %> 
... 
<% f.input :currency %> 
<% f.association :retailer %> 
... 

が含まれていますエラー:

undefined method `retailer_id' for #<Product:0x007ffbe0f7d530> 

私は(明らかに)これに対してかなり新しいですが、これを解決することはできませんでした。

編集:チェック済みマイグレーションを実行していて、最新の状態です。 Retailerテーブルにはidカラムがあります!

> Retailer.all 
Retailer Load (0.2ms) SELECT "retailers".* FROM "retailers" 
=> [#<Retailer id: 1, name: "Retailer 1" etc... 

chemaファイル:

ActiveRecord::Schema.define(:version => 20120308195055) do 

    create_table "alerts", :force => true do |t| 
    t.string "url",  :null => false 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

    create_table "products", :force => true do |t| 
     t.string "title",      :null => false 
     t.integer "price_cents", :default => 0, :null => false 
     t.string "currency",     :null => false 
     t.string "asin",      :null => false 
     t.datetime "created_at",     :null => false 
     t.datetime "updated_at",     :null => false 
    end 

    create_table "retailers", :force => true do |t| 
     t.string "name",  :null => false 
     t.datetime "created_at", :null => false 
     t.datetime "updated_at", :null => false 
    end 

    end 

答えて

0

あなたは、それぞれの製品のためのあなたのモデルとフォームでbelongs_to :Retailer<% f.association :retailer %>を持っているので、あなたはあなたの製品テーブルにretailer_id列を追加する必要があります。

製品作成テーブルの移行ファイルにがありません。なぜ、私は「それは関連せずに動作しますが、それに誤りがundefined method 'retailer_id' for #<Product:0x007ffbe0f7d530>ある」

はい、小売業者テーブルがid列を有しているが、製品から貴社の製品テーブルを小売店を参照するためにあなたを引用され retailer_id列が必要です。