2012-02-21 7 views
0

다음 필드를 사용하여 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
: 아무것도
경로에서 다음 모델에서

답변

1

사용 보기에 아무것도
, 당신은 '중첩'필드, f.fields_for :other_model do |inner|를 사용하려면 다음 사용할 수있는 시점에서 inner.inner_field_name. 제 생각에는 내면이 당신의 경우에 성분이 될 것입니다, 이것이 그것이 RecipieIngredient이고 당신이 그것을 성분으로 바꾸었다면 말이죠. receipieIngredient가 실제로 필요에 따라 더 복잡한 조인 테이블 재실행 인 경우

http://railscasts.com/episodes/196-nested-model-form-part-1

에서 나는 엔티티 '레서피'과 '재료'의 이름을 다음 fields_for :ingredients do |i|이있는 recipie의 형태를 가질 것이다 참조하십시오.

당신이 Ingredient has_many recipiesRecipie has_many ingredients을 원하는 경우에, 당신은 너무와 to_both recipie 및 성분 속하는 가입 모델 IngredientRecipie를 가리키는 모델 모두에서 has_many_through하지만 그 형태에 영향을주지 않습니다 할 수 있습니다. 이러한 관계는 데이터를 검색하는 데 사용됩니다.

has_many through가있는 경우 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 
+0

나는 'recipes has_many foods'와'foods have_many recipes'을 가지고 있고'Recipe_Ingredients'라는 조인 테이블과 함께': through'를 사용합니다. Recipe 모델과 Recipe_Ingredients 조인 모델에'accepts_nested_attributes_for'가 있어야합니까? 아니면 그 중 하나만 받아 들여야합니까? – lordmarinara

+0

배치 방법에 대한 답변이 업데이트되었습니다. 당신의 질문에 : - 당신이 recipie에 '즉시'많은 재료를 추가 할 수 있기를 원한다면'Recipie'에 has_many가 있고'RecipieIngredient'에 첫 번째'belongs_to'가 필요합니다.한꺼번에 여러 가지 레시피에 하나의 재료를 추가하려면 '성분'에 'has_many'가 있어야하고 두 번째로 'belongs_to'가 필요합니다. –

+0

감사합니다. +1 – lordmarinara

1

.

첫 번째는 ActiveRecord 위임을 사용하여 상위 모델에 하위 모델의 특성을 지정하는 것입니다. http://api.rubyonrails.org/classes/Module.html#method-i-delegate

둘째,이 양식의 일부를 다른 모델 개체에 속하게하려면 중첩 된 양식을 사용할 수 있습니다. http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

+0

+1. 비록 그것이 나의 경쟁적인 대답이지만, 대의원을 사용하는 것은 훌륭한 습관입니다. –

관련 문제