Ruby-on-rails Rails 选择助手 - 默认选定值,如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/623458/
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-02 21:02:42  来源:igfitidea点击:

Rails select helper - Default selected value, how?

ruby-on-rails

提问by totocaster

Here is a piece of code I'm using now:

这是我现在正在使用的一段代码:

<%= f.select :project_id, @project_select %>

How to modify it to make its default value equal to to params[:pid]when page is loaded?

如何修改它以使其默认值等于params[:pid]加载页面时?

回答by htanata

This should do it:

这应该这样做:

<%= f.select :project_id, @project_select, :selected => params[:pid] %>

回答by lynx

Use the right attribute of the current instance (e.g. @work.project_id):

使用当前实例的正确属性(例如@work.project_id):

<%= f.select :project_id, options_for_select(..., @work.project_id) %>

回答by Cheng

Rails 3.0.9

导轨 3.0.9

select options_for_select([value1, value2, value3], default)

回答by Mike Bethany

The problem with all of these answers is they set the field to the default value even if you're trying to edit your record.

所有这些答案的问题在于,即使您尝试编辑记录,它们也会将该字段设置为默认值。

You need to set the default to your existing value and then onlyset it to the actual default if you don't have a value. Like so:

您需要将默认值设置为现有值,然后在您没有值时才将其设置为实际默认值。像这样:

f.select :field, options_for_select(value_array, f.object.field || default_value)

For anyone not familiar with f.object.fieldyou always use f.objectthen add your field name to the end of that.

对于不熟悉f.object.field您的任何人,请始终使用f.object然后将您的字段名称添加到该名称的末尾。

回答by gavit

Try this:

尝试这个:

    <%= f.select :project_id, @project_select, :selected => f.object.project_id %>

回答by danengle

if params[:pid] is a string, which if it came from a form, it is, you'll probably need to use

如果 params[:pid] 是一个字符串,如果它来自一个表单,那么你可能需要使用

params[:pid].to_i  

for the correct item to be selected in the select list

在选择列表中选择正确的项目

回答by totocaster

I've found solution and I found that I'm pretty unexperienced in RoR.

我找到了解决方案,但我发现我在 RoR 方面非常缺乏经验。

Inside the controller that manages view described above add this:

在管理上述视图的控制器中添加以下内容:

@work.project_id = params[:pid] unless params[:pid].nil?

回答by jschorr

<%= f.select :project_id, @project_select, :selected => params[:pid] %>

回答by Shelvacu

I couldn't get this to work and found that I needed to add the "selected" html attribute not only to the correct <option>tag but alsoto the <select>tag. MDN's docs on the selected attribute of the select tagsay:

我无法让它工作,发现我不仅需要将“selected”html 属性添加到正确的<option>标签中,而且需要添加到<select>标签中。MDN 关于 select 标签的 selected 属性的文档说:

selected- Boolean attribute indicates that a specific option can be initially selected.

selected- 布尔属性指示可以初始选择特定选项。

That means the code should look like:

这意味着代码应该如下所示:

f.select :project_id, options_for_select(@project_select, default_val), html: {selected: true}

回答by swapnilp

<%= f.select :project_id, options_from_collection_for_select(@project_select,) %>