0

在 Rails 视图中,我试图<select>为许多具有受限值的不同字符串字段显示一个下拉列表。

我一直在尝试使用部分(如下)来执行此操作,但当前值未在<select>列表中选择。

  • 是否可以部分执行此操作?如果是这样,怎么做?
  • 有没有更好的方法可以采取?

编辑.html.erb:

<% form_for(@my_class) do |f| %>
  <%= render :partial => "select", :locals => { :attribute_name => :blah, :f => f } %>
<% end %>

_select.html.erb:

<p>
  ...
  <%= f.label attribute_name %><br />
  <%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => attribute_name } %>
  ...
</p>
4

1 回答 1

0

I believe the selected option checks based on the value, not the name of the attribute.

This may work for you, but I have not tested it out:

<%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => @my_class.send(attribute_name) } %>
于 2010-07-27T14:27:15.213 回答