【发布时间】:2012-03-12 07:43:30
【问题描述】:
我正在尝试使用以下字段创建一个 form_for。但是,有些字段对应一个模型,RecipeIngredient 类(模型),而底部字段对应于 Recipe 类(模型)。
<%= form_for(:recipe_and_ingredients, :url => {:action => 'create'}) do |f| %>
<!-- RecipeIngredient classs -->
<table>
<tr>
<td>4</td>
<td><%= f.text_field(:quantity) %></td>
<td><%= f.text_field(:weight) %></td>
<td><%= f.text_field(:units) %></td>
</tr>
</table>
<!-- Recipe class -->
<table summary="Recipe form fields">
<tr>
<th>Recipe Name</th>
<td><%= f.text_field(:recipe_name) %></td>
<th>Total Grams</th>
<td><%= f.text_field(:total_grams) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Create Recipe") %>
</div>
<% end %>
但是,在我的控制器中:
# Instantiate a new object using form parameters
@recipe = Recipe.new(params[:recipe_and_ingredients])
@ingredient = RecipeIngredient.new(params[:recipe_and_ingredients])
我在提交表单时收到此错误
unknown attribute: quantity
我知道这是因为配方模型/类/表/任何东西都没有数量列;这是RecipeIngredient 中的一列(如果相关,它应该是Recipe 和其他表之间的连接表)。但是,当我提交参数时,如何在代码中区分哪些参数属于哪个类?
【问题讨论】:
标签: html ruby-on-rails forms model-view-controller class