模型
class Task < ActiveRecord::Base
attr_accessible :deadline, :list_id, :name, :summary
validates_presence_of :deadline, :list_id, :name, :summary
belongs_to :list
end
class List < ActiveRecord::Base
attr_accessible :name
has_many :tasks, :dependent => :destroy
end
<%= collection_select(:post, :list_id, List.all, :id, :name, :prompt => true) %>
渲染:
<select id="post_list_id" name="post[list_id]">
<option value="">Please select</option>
<option value="1">House</option>
<option value="2">Car</option>
</select>
我将list_id设置为必需,当我在添加任务时从下拉列表中选择列表(list_id)时,它表示未选择list_id。我能做错什么?
更新:想通了,不得不修改表单视图中的collection_select行,我也没有包含f。部分到它
f.collection_select(:list_id, List.all, :id, :name, {:include_blank => true})