2010-08-24 60 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