Ruby on Rails -- f.select 中的多项选择 [英] Ruby on Rails -- multiple selection in f.select

查看:68
本文介绍了Ruby on Rails -- f.select 中的多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有以下选择框:

I have the following select box in my form:

Related Type: &nbsp; <%= f.select(:TYPE, [['Type A', 'Type A'],
                                  ['Type B', 'Type B'],
                                  ['Type C', 'Type C'],
                                  ['Type D', 'Type D'],
                                  ['Type E', 'Type E']
                                 ],{ :prompt => "Please select"}
                                 ) %>

我想允许用户进行多项选择,并将选择框的大小设为 5.

I want to allow the user to make multiple selections and also make the size of the select box 5.

如何为上面的代码做到这一点?

How to do that for the code above?

推荐答案

After your { :prompt =>请选择"} 添加另一个带有 html 选项的哈希,例如

After your { :prompt => "Please select"} add another hash with html options e.g.

<%= f.select(:TYPE, [['Type A', 'Type A'],
                                  ['Type B', 'Type B'],
                                  ['Type C', 'Type C'],
                                  ['Type D', 'Type D'],
                                  ['Type E', 'Type E']
                                 ],{ :prompt => "Please select"},
                                   { :multiple => true, :size => 5 }
                                 ) %>

完成此操作后,您可能希望移动 :prompt 选项(但保留空的 {} 以便 html 属性不会被视为 Rails选项.)

Once you've done this you might want to move your :prompt option (keep the empty {} though so that html attributes don't get treated as Rails options.)

此外,您还需要确保您的控制器代码正确接受和处理多个值.

Also you'll need to ensure your controller code is correctly accepting and handling multiple values.

这篇关于Ruby on Rails -- f.select 中的多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆