Ruby-on-rails 选择标签,指定选定的选项(或将数组元素移动到索引 0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4848032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:15:03  来源:igfitidea点击:

Select tag, specifying the selected option (or moving an array element to index 0)

ruby-on-rails

提问by Reno

Say I'm querying a list of fruit and then collecting just the id's and name's of the fruit into @fruit.

假设我正在查询一个水果列表,然后只将水果的 id 和名称收集到 @fruit 中。

[32, "apple"],
[8, "bannana"],
[10, "cantelope"],
[11, "grape"],
[15, "orange"],
[41, "peach"],
[22, "watermelon"]

@fruit is being used in a Select helper. "apple" being at index 0 of @fruit will be the selected value (first option) of the select. This is a made up example but by default I'll always know what "orange" is by name (not by id). I need "orange" to be the selected value of the select tag (the first option).

@fruit 正在 Select 助手中使用。位于@fruit 索引 0 处的“apple”将是选择的选定值(第一个选项)。这是一个虚构的示例,但默认情况下,我将始终通过名称(而不是 id)知道“橙色”是什么。我需要“orange”作为选择标签的选定值(第一个选项)。

":prompt => 'orange'" just adds a 2nd instance of "orange" in the select. Everything I've found on Google so far seems to be about adding an extra value or blank to the list.

":prompt => 'orange'" 只是在选择中添加了第二个 "orange" 实例。到目前为止,我在 Google 上找到的所有内容似乎都是关于在列表中添加一个额外的值或空白。

Since index 0 of the array always becomes the selected value (if no prompt or blank is used in the select helper), is there a way I could find the index containing the name "orange" (@fruit[x].name == 'orange'), and move it to index 0 while retaining the existing alpha sort in the rest of the list? So, the @fruit array would look like:

由于数组的索引 0 始终成为选定值(如果在选择帮助器中没有使用提示或空白),有没有办法找到包含名称“orange”的索引(@fruit[x].name == 'orange'),并将其移动到索引 0,同时在列表的其余部分保留现有的 alpha 排序?所以,@fruit 数组看起来像:

@fruit[0]    [15, "orange"],
@fruit[1]    [32, "apple"],
@fruit[2]    [8, "bannana"],
@fruit[3]    [10, "cantelope"],
@fruit[4]    [11, "grape"],
@fruit[5]    [41, "peach"],
@fruit[6]    [22, "watermelon"]

The only thing I can think of right now would be to iterate through @fruit and if "orange" is found add it to a new array. Then iterate through the @fruit array again and add anything that doesn't have a name of "orange". I'm not sure how to write that out but it seems like it would do what I'm looking for. Maybe there's some easy way to do this I'm missing (specifying which index in an Array is to be the first option written)?

我现在唯一能想到的就是遍历@fruit,如果找到“orange”,则将其添加到新数组中。然后再次遍历@fruit 数组并添加任何名称不为“orange”的内容。我不知道如何写出来,但似乎它会做我正在寻找的东西。也许有一些简单的方法可以做到这一点我错过了(指定数组中的哪个索引是第一个写入的选项)?

Thank You!

谢谢你!

回答by ctcherry

See here: http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease

请参阅此处:http: //guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease

If you use the existing helpers, you can specify by id which option you want selected. Specifically this example:

如果您使用现有的助手,您可以通过 id 指定要选择的选项。具体这个例子:

<%= options_for_select([['Lisbon', 1], ['Madrid', 2]], 2) %>

output:

<option value="1">Lisbon</option>
<option value="2" selected="selected">Madrid</option>

You say you don't know which option is "orange" by ID. If you want to find the ID for "orange" in the fruit array this would do it, then you can use the helper:

你说你不知道 ID 哪个选项是“橙色”。如果您想在水果数组中找到“orange”的 ID 就可以了,那么您可以使用帮助程序:

(@fruit.detect { |i| i[1] == 'orange' } || []).first

回答by Syed Aslam

You can use options_from_collection_for_selectin the select tag for selecting a default option. For example:

您可以options_from_collection_for_select在 select 标签中使用来选择默认选项。例如:

<%= select_tag 'state',
    options_from_collection_for_select(@fruits, 'id', 'name', 15),
    :include_blank => true %>

This would select 'Orange' by default. API Docs.

这将默认选择“橙色”。API 文档