2010-07-27 84 views
0

在Rails视图中,我试图显示一个<select>下拉列表,其中包含许多具有受限值的不同字符串字段。是否有可能将属性绑定到部分中的forms_helper.select?

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

  • 是否有可能在部分做到这一点?如果是这样,怎么样?
  • 有更好的方法吗?

edit.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> 

回答

0

我相信基于价值,而不是名称选定选项检查属性。

这可能会为你工作,但我还没有测试出来:

<%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => @my_class.send(attribute_name) } %> 
+0

感谢。这比我预期的更复杂,但如果它起作用,它就会起作用。希望我会在本周晚些时候尝试。 – Armand 2010-08-02 16:51:47

相关问题