다음 필드를 사용하여 form_for를 만들려고합니다. 그러나 일부 필드는 하나의 모델 인 RecipeIngredient 클래스 (모델)에 해당하는 반면 맨 아래 필드는 Recipe 클래스 (모델)에 해당합니다.레일에서 form_for에 여러 클래스 사용
<%= 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와 다른 테이블 간의 조인 테이블로 간주됩니다). 그러나 매개 변수를 제출할 때 어떤 매개 변수가 어떤 클래스에 속하는지 코드에서 어떻게 구별합니까? 컨트롤러에서 accepts_nested_attributes_for
: 아무것도
경로에서 다음 모델에서
나는 'recipes has_many foods'와'foods have_many recipes'을 가지고 있고'Recipe_Ingredients'라는 조인 테이블과 함께': through'를 사용합니다. Recipe 모델과 Recipe_Ingredients 조인 모델에'accepts_nested_attributes_for'가 있어야합니까? 아니면 그 중 하나만 받아 들여야합니까? – lordmarinara
배치 방법에 대한 답변이 업데이트되었습니다. 당신의 질문에 : - 당신이 recipie에 '즉시'많은 재료를 추가 할 수 있기를 원한다면'Recipie'에 has_many가 있고'RecipieIngredient'에 첫 번째'belongs_to'가 필요합니다.한꺼번에 여러 가지 레시피에 하나의 재료를 추가하려면 '성분'에 'has_many'가 있어야하고 두 번째로 'belongs_to'가 필요합니다. –
감사합니다. +1 – lordmarinara