Rails的collection_select帮助方法和最后的"创建项目"选项

and*_*ndi 12 html ruby-on-rails

是否可以使用辅助方法在创建<option>的末尾添加?<select>collection_select

现在我有

f.collection_select(:category_id , @categories, :id, :name, {:prompt => 'Please select a category'})
Run Code Online (Sandbox Code Playgroud)

产生

<select id="product_category_id" name="product[category_id]">
  <option value="">Please select a category</option>
  <option value="7">category one</option>
  <option value="8">category 2</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我想要的是什么

<select id="product_category_id" name="product[category_id]">
  <option value="">Please select a category</option>
  <option value="7">category one</option>
  <option value="8">category 2</option>
  <option value="new">..or create a new one</option>
</select>
Run Code Online (Sandbox Code Playgroud)

这是可能的还是我应该循环遍历集合并手动生成选项?

Gde*_*lin 11

你可能应该使用select.

像这样:

f.select(:category_id, @categories.collect {|p| [ p.name, p.id ] } + [ [ 'Or create a new one', 'new' ] ], {:include_blank => 'Please select a category'})
Run Code Online (Sandbox Code Playgroud)

祝好运!