2012-03-02 41 views
0

在Rails 3.2中,在嵌套表单中包含选择字段的正确方法是什么?如何在嵌套窗体中定义选择字段?

我现在有

//IN THE PARENT FORM 

<%= f.fields_for :crayons do |crayon| %> 
    <%= render 'caryon_fields', :f=>crayon %> 
<% end %> 

//IN THE PARTIAL 

<div class="nested-fields"> 
    <%= select (:crayon, :color, [['Red',1],['Blue',2],['Yellow',3],['Green',4]]) %> 
</div> 

这不是保存所选值到数据库中。我认为这是因为建设者没有被通过。

我应该如何在嵌套字段中定义具有硬编码选项的选择字段?是否需要更改标签,即select_tagcollection_select。我仍然不确定所有这些之间的差异。如果任何人都可以指出我明确的描述,将不胜感激。

谢谢!

回答

1

您需要关联到你的形式在部分这样的:

<div class="nested-fields"> 
    <%= f.select (:crayon, :color, [['Red',1],['Blue',2],['Yellow',3],['Green',4]]) %> 
</div> 

Whitout部分是:

<%= f.fields_for :crayons do |crayon| %> 
    <div class="nested-fields"> 
    <%= crayon.select (:crayon, :color, [['Red',1],['Blue',2],['Yellow',3],['Green',4]]) %> 
    </div> 
<% end %> 
+0

感谢shingara。我已经尝试过了。它返回'undefined method“merge”'。我不认为这是正确的语法。如果<%= select(f,:color,...%>,不是吗?但这也行不通。 – 2012-03-02 08:51:26

+0

事实上,这就是解决方案,我只是简单的''= f.select(:color,[['red',1] .....%>''我需要删除模型引用,而不是用构建器替换它谢谢你让我按照另一条线思考。 – 2012-03-02 08:56:12

相关问题