简体   繁体   English

Ruby on rails用f.select下拉数组

[英]ruby on rails drop down of array with f.select

I have an array like this: 我有一个像这样的数组:

@array = [[1, a], [2, b], [3, c] ............]

i want to create a drop down in my view with an 我想在视图中创建一个下拉菜单

f.select 

the drop down should have only the numbers ie, [1, 2, 3, ....] 下拉列表应仅包含数字,即[1, 2, 3, ....]

How can i create a drop down with only the numbers and an f.select in my view? 如何在视图中仅包含数字和f.select的下拉列表?

这样的事情会起作用:

@array.collect{|x| x[0]}

Just use. 随便使用。 It is very easy to implement. 这很容易实现。

select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) select(:person,:city_id,[['Lisbon',1],['Madrid',2],...])

or 要么

f.select(:city_id, [['Lisbon', 1], ['Madrid', 2], ...]) f.select(:city_id,[['Lisbon',1],['Madrid',2],...])

Notice that the third parameter, the options array, is the same kind of argument you pass to options_for_select. 注意,第三个参数options数组与传递给options_for_select的参数类型相同。 One advantage here is that you don't have to worry about pre-selecting the correct city if the user already has one — Rails will do this for you by reading from the @person.city_id attribute. 这里的一个优点是,如果用户已经拥有一个城市,则不必担心预先选择正确的城市-Rails会通过读取@ person.city_id属性为您完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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