Options_for_select - 如何选择数据库中的内容?

发布于 2024-09-28 05:12:31 字数 466 浏览 2 评论 0原文

我有一个复杂的表单(如 Ryan B 的 Complex Form Railscasts),其中同时更改了几个级别的数据库表。

该下拉框的代码的工作原理是将正确的整数传递到数据库。但是,尽管进行了多次尝试,我仍无法让它正确反映数据库的当前值。我该如何整理这段代码?

<%= o.select :weighting, options_for_select([
  ["Correct", "4", {:class=>"bold"}],
  ["Good", "3"],
  ["Average", "2"],
  ["Poor", "1"], 
  ["Incorrect", "0", {:class=>"bold"}] ], :weighting), {},
  html_options = {:class => "listBox", :style=>"float:left;"} %>

谢谢。

I have a complex form (like Ryan B's Complex Form Railscasts) where I have a few levels of database tables being altered at the same time.

The code for this dropdown box works in that it delivers the correct integer to the database. But, despite numerous attempts I cannot get it to correctly reflect the database's CURRENT value. How can I sort out this code?

<%= o.select :weighting, options_for_select([
  ["Correct", "4", {:class=>"bold"}],
  ["Good", "3"],
  ["Average", "2"],
  ["Poor", "1"], 
  ["Incorrect", "0", {:class=>"bold"}] ], :weighting), {},
  html_options = {:class => "listBox", :style=>"float:left;"} %>

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身狗的梦 2024-10-05 05:12:31

你走在正确的轨道上,但还没有完全实现。

options_for_select 的最后一个参数应该是所选选项的值。您提供的值 :weighting 是一个与任何给定选项的值都不匹配的符号。

您需要提供实际值。如果您使用实例对象来构建表单,如

<%form_for @whatever do |o|%>
...

您可以简单地使用 @whatever.weighting.to_s ,如:

<%= o.select :weighting, options_for_select([
  ["Correct", "4", {:class=>"bold"}],
  ["Good", "3"],
  ["Average", "2"],
  ["Poor", "1"], 
  ["Incorrect", "0", {:class=>"bold"}] ], @whatever.weighting.to_s), {},
  html_options = {:class => "listBox", :style=>"float:left;"} %>

否则,有一种方法可以将对象从表单块变量 o 中获取。但这会扰乱内部结构,而内部结构可能会随着升级而改变。

编辑:如果您正在处理多个部分的字段,则可以使用对象访问器从表单构建器块变量中获取特定对象。

重用上面的示例,类似这样使用表单的该实例部分中每个子实例的当前权重。

<% form_for @parent do |p| %>
  ...
  <% p.fields_for :children do |c| %>
  ...
  <%= c.select :weighting, options_for_select([
      ["Correct", "4", {:class=>"bold"}],
      ["Good", "3"],
      ["Average", "2"],
      ["Poor", "1"], 
      ["Incorrect", "0", {:class=>"bold"}] ], c.object.weighting.to_s), {},
      html_options = {:class => "listBox", :style=>"float:left;"} %>
  ...
  <% end %>
<% end %>

这也可以用在部分中。

You're on the right track, but not quite there.

While the final argument to options_for_select should be the value of the selected option. The value you supply :weighting is a symbol that does not match the value of any of your given options.

You will need to give the actual value. If you used an instance object to build the form as in

<%form_for @whatever do |o|%>
...

You can simply used @whatever.weighting.to_s as in:

<%= o.select :weighting, options_for_select([
  ["Correct", "4", {:class=>"bold"}],
  ["Good", "3"],
  ["Average", "2"],
  ["Poor", "1"], 
  ["Incorrect", "0", {:class=>"bold"}] ], @whatever.weighting.to_s), {},
  html_options = {:class => "listBox", :style=>"float:left;"} %>

Otherwise, there's a way to get the object off the form block variable o. But that's messing with internals which may change with an upgrade.

Edit: In the case where you're working with fields for and multiple partials, you can get the particular object off of the form builder block variable.with the object accessor.

Reusing the above example something like this to use the current weighting of each child instance in that instance's section of the form.

<% form_for @parent do |p| %>
  ...
  <% p.fields_for :children do |c| %>
  ...
  <%= c.select :weighting, options_for_select([
      ["Correct", "4", {:class=>"bold"}],
      ["Good", "3"],
      ["Average", "2"],
      ["Poor", "1"], 
      ["Incorrect", "0", {:class=>"bold"}] ], c.object.weighting.to_s), {},
      html_options = {:class => "listBox", :style=>"float:left;"} %>
  ...
  <% end %>
<% end %>

This can also be used in partials.

木森分化 2024-10-05 05:12:31

第二次尝试 =)

<%= f.label :priority %>
<%= f.select(:priority, options_for_select({"Stat" => "1", "Urgent" => "2", "Regular" => "3", "Safety" => "4"}, @servicerequest.priority), :prompt => "Choose") %>

Second try =)

<%= f.label :priority %>
<%= f.select(:priority, options_for_select({"Stat" => "1", "Urgent" => "2", "Regular" => "3", "Safety" => "4"}, @servicerequest.priority), :prompt => "Choose") %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文