【问题标题】:Using multiple classes in a form_for Rails在 form_for Rails 中使用多个类
【发布时间】: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


    【解决方案1】:

    使用 模型内:accepts_nested_attributes_for
    在控制器中:无
    在路线中:没有
    在视图中,在您想要“嵌套”字段的位置,使用f.fields_for :other_model do |inner|,然后您可以使用inner.inner_field_name。我认为内部将是您的情况的成分,如果那是 RecipieIngredient 是什么并且您将其重命名为成分。如果 receipieIngredient 实际上是更复杂的连接表,则根据需要重做。

    http://railscasts.com/episodes/196-nested-model-form-part-1查看更多信息

    我将实体命名为“recipies”和“ingredients”,然后创建一个包含fields_for :ingredients do |i| 的recipie 表单。

    如果您想要Ingredient has_many recipiesRecipie has_many ingredients,您也可以在两个模型中使用has_many_through 来执行此操作,这些模型都指向一个连接模型IngredientRecipie,它属于_recipie 和成分,但这不会影响表单.这些关系用于检索数据。

    请注意,如果您拥有这些 has_many ,则可以使用 simple_form 来维护关系,并使用复选框来维护关系,而无需编写其他代码,只需一次保存即可保存数据。

    更新:

    Recipie (model)
    Recipie has_many :recipie_ingredients
    Recipie has many :ingredients, :through => :recipie_ingredients
    
    Ingredient (model)
    Recipie has_many :recipie_ingredients
    Recipie has many :ingredients, :through => :recipie_ingredients
    
    RecipieIngredient (model)
    belongs_to :recipies
    belongs_to :ingredients
    

    【讨论】:

    • 我有recipe has_many foodsfoods has_many recipes,我使用:through 和一个名为Recipe_Ingredients 的连接表。我应该在 Recipe 模型和 Recipe_Ingredients 加入模型中有accepts_nested_attributes_for,还是其中之一?
    • 我更新了答案的布局方式。对于您的问题:- 如果您希望能够“一次”添加许多成分到一个食谱中,那么您需要Recipie 中的has_manyRecipieIngredient 中的第一个belongs_to。如果您希望能够一次将单一成分添加到一堆 Recipies,那么您需要 Ingredients 中的 has_many 和第二个 belongs_to
    【解决方案2】:

    有两种方法可以解决这个问题。

    第一个是使用 ActiveRecord 委托将子模型的属性放在父模型上。您可以在此处查看有关如何执行此操作的文档:http://api.rubyonrails.org/classes/Module.html#method-i-delegate

    其次,您可以使用嵌套表单使该表单的一部分属于不同的模型对象。您可以在此处查看相关文档:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

    【讨论】:

    • 为此 +1。尽管这是我的一个相互竞争的答案,但使用委托是一种很好的做法。
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多