在options_from_collection_for_select中为选项添加一个类

时间:2011-03-21 09:19:23

标签: ruby-on-rails-3

我想在select标签中的一个选项元素中添加一个类(“has-versions”),具体取决于选项。我该如何添加该类?

 = select_tag 'project_version_id', "<option value='' disabled>-optional select version-</option>".html_safe + options_from_collection_for_select(current_user.current_project.project_versions, :id, :name, @test_case.script.try(:project_version_id))

我需要它是这样的:

<select name="project_version_id" id="project_version_id">
  <option disabled="" value="">-optional select version-</option>
  <option selected="selected" value="19">NOOB SAIBOT</option>
  <option class="has-versions" value="20">PEW PEW</option>
</select>

1 个答案:

答案 0 :(得分:6)

以下是让您入门的内容: 2项改进:

  • 使用:include_blank作为&#34;入门者&#34;值
  • 在生成选择
  • 的选项时传递:class

<%= select 
  "new_something", 
  "project_version_id", 
  options_for_select(["Item11", ["Item 2",{:class=>'has-versions'}]]),
  {:include_blank => '-optional select version-'} %>

提示:您可以在数组上使用collect根据需要生成options_for_select