使用options_for_select为f.select设置include_blank

时间:2016-07-06 03:42:53

标签: ruby-on-rails ruby-on-rails-4

我的观点中有以下内容:

<%= f.select :blood_group, options_for_select([
              ['O+', Membership.blood_groups.keys[0]], ['O-', Membership.blood_groups.keys[1]],
              ], @user.blood_group) %>

如何在上面添加include_blank属性?当我在最后附加它时,我得到错误“错误的参数数量(给定3,预期1..2)”

2 个答案:

答案 0 :(得分:1)

你可以这样做:

<%= f.select :blood_group, [['', nil], ['one', 1], ['two', 2], ['three', 3], ['four', 4], ['five', 5], ['', nil]]

可选择指定options_for_select,注意这会在开头和结尾添加两个空白字段。

答案 1 :(得分:0)

您可以执行以下操作:

<%= f.select :blood_group, options_for_select([
    ['O+', Membership.blood_groups.keys[0]], 
    ['O-', Membership.blood_groups.keys[1]]], @user.blood_group, {include_blank: true}, {}) %>

您可以参考文档:options_for_select