tanya
tanya

Reputation: 2985

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

Upvotes: 1

Views: 1687

Answers (1)

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47913

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.

Upvotes: 3

Related Questions