2017-02-23 66 views
2

我正在使用Rails 5和bootstrap_form_for来构建表单。添加类以在Rails 5中选择字段with bootstrap_form_for

我知道有在于是一些类似的问题,但没有一个帮我解决我的问题...

我想自定义类添加到选择字段。但我的是这样的:

<%= f.collection_select :location_id, Location.all, :id, :name, :include_blank => ("Insere um endereço ou escolha um local da lista..."), hide_label: true, :class => 'location' %> 

但该类不适用。我可以做什么不同?

+0

尝试'<%= f.collection_select:LOCATION_ID,Location.all,:ID,:名称,{},{类: “位置”}%>' –

回答

2
<%= f.collection_select(:location_id, Location.all, :id, :name, {:include_blank => ("Insere um endereço ou escolha um local da lista..."), hide_label: true}, class: "class-name") %> 
+1

无效的语法,你需要关闭正常在最后一个选项之后加强。总是有用的做,尽可能明确这些选项哈希! – BookOfGreg

+1

这工作,但我不得不移动到最后。 – almo

+0

@BookOfGreg良好的捕获 - 现在更新。谢谢! – gwalshington

1

您需要为options部分方法参数添加一个空的散列。这恐怕不是很直观,但如果你看看method signature,你可以看到它为什么需要它。

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


<%= f.collection_select :location_id, 
    Location.all, 
    :id, 
    :name, 
    :include_blank => ("Insere um endereço ou escolha um local da lista..."), 
    hide_label: true, 
    {}, 
:class => 'location' %>