繁体   English   中英

如何在Rails中为collection_select设置HTML选项?

[英]How do I set the HTML options for collection_select in Rails?

我似乎无法找到将类添加到由Rails collection_select生成的select标记的语法。 一些帮助?

许多Rails助手采用多个哈希参数。 第一个通常是控制帮助器本身的选项,第二个是html_options,您可以在其中指定自定义ID,类等。

方法定义如下所示:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

你会注意到参数列表中的多个'= {}'。 要使用它,您要指定的第一组选项实际上必须用大括号括起来:

collection_select(:user, :title, UserTitle.all, :id, :name, {:prompt=>true}, {:class=>'my-custom-class'})

如果除了html类之外没有任何指定选项,那么只需添加一个空的哈希占位符:

collection_select(:user, :title, UserTitle.all, :id, :name, {}, {:class=>'my-custom-class'})

有关其他API文档,请访问: http//apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

= f.collection_select :category_id, Category.order(:name), :id, :name, {}, {class: "store-select"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM