Rails的collection_select帮助器方法和“Create item”选项 [英] Rails' collection_select helper method and the "Create item" option at the end

查看:76
本文介绍了Rails的collection_select帮助器方法和“Create item”选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



现在我有

  f.collection_select(:category_id,@categories,:id,:name,{:prompt =>'请选择一个类别'})

它会产生

 < select id =product_category_idname =product [category_id]> 
< option value =>请选择一个类别< / option>
< option value =7> category one< / option>
< option value =8>类别2< / option>
< / select>

和我想要的是

 < select id =product_category_idname =product [category_id]> 
< option value =>请选择一个类别< / option>
< option value =7> category one< / option>
< option value =8>类别2< / option>
< option value =new> ..或创建一个新的< / option>
< / select>

这是可能的还是应该循环访问并手动生成选项?

解决方案

您应该使用 select 来代替。



像这样:

  f.select(:category_id,@ categories.collect {| p | [p .name,p.id]} + [['或创建一个新的','new']],{:include_blank =>'请选择一个类别'})



祝你好运!


Is it possible to add an <option> at the end of a <select> created with the collection_select helper method?

Right now I have

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

which generates

<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>

and what I would like is

<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>

Is this possible or should I just loop through the collection and generate the options manually?

解决方案

You should probably use select instead.

Like so:

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

Good luck!

这篇关于Rails的collection_select帮助器方法和“Create item”选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆