Adding a css class to select in Rails

人走茶凉 提交于 2019-12-12 02:18:26

问题


I've been trying to make the following class applies on the select/dropdown, but I couldn't!

 = f.select attr, [1,2,3],  class: 'form-control', include_blank: true

I tried to read the rails code, here and here. it's like it is not implemented ? but it works for text fields:

 = f.search_field attr, options.merge(class: 'form-control')

I'm using slim and simple_form


回答1:


select takes 2 hash options, first for 'select options' and another for 'html'.

Try following:

= f.select(attr, [1,2,3], { include_blank: true }, { class: 'form-control' })



回答2:


In this way you can add class and Inline CSS to select tag

<%= select_tag :db_field, options_for_select(@users.select([:email, 'users.id']).map{|user| [user.email, user.id]}), class: 'class_name_1 class_name_2', style: "min-width: 100px;" %>


来源:https://stackoverflow.com/questions/37190047/adding-a-css-class-to-select-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!