I can't seem to find the syntax to add a class to a select tag generated by Rails collection_select
. Some help?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
The method definition looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
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'})
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'})
Additional API documentation is available at: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
回答2:
= f.collection_select :category_id, Category.order(:name), :id, :name, {}, {class: "store-select"}