如何在Rails中设置collection_select的HTML选项? [英] How do I set the HTML options for collection_select in Rails?
问题描述
我似乎找不到添加一个类到Rails生成的select标签的语法 collection_select
。一些帮助?
I can't seem to find the syntax to add a class to a select tag generated by Rails collection_select
. Some help?
推荐答案
许多Rails帮助者需要多个哈希参数。第一个通常是控制帮助器本身的选项,第二个是html_options,您指定自定义ids,类等。
Many Rails helpers take multiple hash arguments. The first is usually the options to control the helper itself, and the second is the html_options where you specifiy custom ids, classes etc.
方法定义如下所示: / p>
The method definition looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
你会注意到params列表中的多个'= {}'。要使用它,您要指定的第一组选项实际上必须括在大括号中:
You'll notice the multiple '= {}' in the params list. To use this, the first set of options that you would specify must actually be enclosed in braces:
collection_select(:user, :title, UserTitle.all, :id, :name, {:prompt=>true}, {:class=>'my-custom-class'})
如果您没有任何选项可以指定html类除外,那么只需放一个空的哈希占位符:
If you don't have any options to specify besides the html class, then just put an empty hash placeholder:
collection_select(:user, :title, UserTitle.all, :id, :name, {}, {:class=>'my-custom-class'})
其他API文档可在:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
Additional API documentation is available at: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
这篇关于如何在Rails中设置collection_select的HTML选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!