楷模
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})