简体   繁体   中英

collection_select set option value of :include_blank to zero

In my rails app, i have a drop down box where i retrieve all groups from the Group table and display them using the collection_select tag.

When the user selects 'None', I want to pass '0' as option value.

Currently, an empty string is passed.

Is there a way to include option value = 0 for 'None'?

<%= f.collection_select :SUB_GROUP, Group.all, :Group_ID, :Group_ID, :include_blank => 'None' %>

Many many thanks for any suggestion provided

If you use options_for_select in combination with select_tag you can achieve that using this:

options_for_select(
   [['None', '0']].concat(
      Group.all.collect { |g| [g.group_id.to_s, g.group_id.to_s] }
   )
)

In order to keep your views uncluttered, you might want to generalize and move this into a helper method with a reasonable name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM