开发者

collection_select set option value of :include_blank to zero

In my ra开发者_如何学Goils 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜