2010-08-24 61 views
0

我在這裏瘋了。無法將collection_select保存到對象

我有:

class Course 
    belongs_to :commune 
end 

class Commune 
    has_many :courses 
end 

在用於課程我有一個下拉新視圖,其中用戶選擇公社:

f.collection_select(:commune, get_commune_list, :id, :commune, { :prompt => true }) 

(該get_commune_list是返回Commune對象列表的幫助器方法)

但是當我嘗試它,我救不出現此錯誤:

Commune(#2176182100) expected, got String(#2148246520)

或公社不保存在課程對象上的。

的參數是這樣的:

{"course"=>{"price"=>"6000", 
"title"=>"Some title", 
"commune"=>"10", 
... 
} 

我只是'†弄明白爲什麼這是不行的!

回答

0

不是嗎?

f.collection_select (:commune, :commune_id, get_commune_list, :id, :commune, { :prompt => true }) 
+0

如果我這樣做,我得到「未定義的方法合併」爲:commune:Symbol「。 如果我使用f.collection_select(:commune_id,get_commune_list,:id,:commune,{:prompt => true}),我爲#得到未定義的方法'commune_id'。 – fiskeben 2010-08-24 17:38:29

+0

你很多嘗試做類似f.select(「commune_id」,Commune.all.collect {| c | [c.name,c.id]},{:prompt => true}) – AMIT 2010-08-24 18:29:31

+0

嘆息...我忘了將commune_id添加到數據庫。花了十四個小時。 感謝您的回答。既然你讓我走上正軌,我會接受它。 – fiskeben 2010-08-24 18:48:35