1
<%= f.collection_select :admin_attachment_id, @data['attachments'], :id, :title, { prompt: 'Select banner' }, { class: 'form-control', required: '', 'ng-model': 'banner.admin_attachment_id', 'ng-change': 'get_attachment_thumbnail()', disabled: 'Select banner', selected: '' } %>

渲染:

<select class="form-control ng-pristine ng-untouched ng-invalid ng-invalid-required" required="required" ng-model="banner.admin_attachment_id" ng-change="get_attachment_thumbnail()" name="admin_banner[admin_attachment_id]" id="admin_banner_admin_attachment_id">
    <option value="">Select banner</option>
    <option value="89">Banner 1</option>
    <option value="94">Banner 2</option>
    <option value="114">Banner 3</option>
</select>

我正在尝试将第一个选项设置为选定项目,但必须禁用。我怎样才能做到这一点?

4

2 回答 2

0

根据http://apidock.com/rails/v4.2.1/ActionView/Helpers/FormOptionsHelper/select

默认情况下, post.person_id 是选定的选项。指定 selected: value 以使用不同的选择或 selected: nil 使所有选项保持未选中状态。

因此,您将输入 selected: nil 并删除 disabled :

<%= f.collection_select :admin_attachment_id, @data['attachments'], :id, :title, { prompt: 'Select banner', selected: nil }, { class: 'form-control', required: '', 'ng-model': 'banner.admin_attachment_id', 'ng-change': 'get_attachment_thumbnail()' }

我对此进行了反复讨论collection_select,因为该方法的文档中未提及该方法,而仅针对select.

希望这可以帮助!

于 2016-03-13T04:46:32.953 回答
0

我在 a 中使用它select_tag,但您需要一种形式的分组选项:

grouped_options = [ 
  [ 'Select banner',
    [
     ['Banner 1', '89'],
     ['Banner 2', '94'],
     ['Banner 3', '114']
    ]
  ]

select_tag('admin_banner[admin_attachment_id]', grouped_options_for_select(:grouped_options))
于 2016-02-04T13:14:07.617 回答