【问题标题】:Unable to add css class in simple-form select无法在简单形式选择中添加 css 类
【发布时间】:2019-06-19 07:09:23
【问题描述】:

我正在尝试以简单的形式为选择字段添加 CSS,但它不适用。我尝试了以下几种方式,

= f.select :user_types, options_for_select([['Type', ''], ['College', 'admin'], ['Student', 'student']], {required: true, selected: ''}), input_html: {id: 'college_admin_type', class: 'form-control custom-drop-icon'}

= f.select :user_types, options_for_select([['Type', ''], ['College', 'admin'], ['Student', 'student']], {required: true, selected: ''}), wrapper_html: {id: 'college_admin_type', class: 'form-control custom-drop-icon'}

= f.select :user_types, options_for_select([['Type', ''], ['College', 'admin'], ['Student', 'student']], {required: true, selected: ''}), id: 'college_admin_type', class: 'form-control custom-drop-icon'

我找到了类似的答案,但无法弄清楚。这是一个链接Adding a css class to select in Rails

【问题讨论】:

    标签: css ruby-on-rails simple-form slim-lang


    【解决方案1】:

    如果您使用simple_form,则应使用其as: :select 表示法

    = f.input :user_types, as: :select,
      collection: [['Type', ''], ['College', 'admin'], ['Student', 'student']],
      input_html: { id: 'college_admin_type', class: 'form-control custom-drop-icon' }
    

    但如果您使用默认的 rails select 助手,您应该将 html 选项作为第二个选项哈希传递。

    = f.select(:user_types, [['Type', ''], ['College', 'admin'], ['Student', 'student']], {}, { class: 'form-control custom-drop-icon' })
    

    另见Ruby on Rails form_for select field with class

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      相关资源
      最近更新 更多