开发者

add a class to an option in options_from_collection_for_select

I want to add a class("has-versions) to an option element in my select tag depending on the option item. How can I add that class?

 = 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))

I need it to be something like this:

<select name="p开发者_如何学Pythonroject_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>


Here is something to get you started: 2 improvements:

  • Use :include_blank for a "starter" value
  • Pass the :class when generating the options for select

.

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

Hint: You can use collect on an array to generate the options_for_select as desired.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜