2012-06-13 1 views
0

저는 join model을 통해 has_many에서 속성을 편집하는 것에 대해 고민하고 있습니다. 실험 할 수있는 아주 간단한 앱을 만들었습니다. 조리법, 재료 및 조리법 - 조리법 (조인).Rails 3.1 - 조인 모델에서 속성 편집하기?

누구나이 작업을 도와 줄 수 있습니까? 그것은 그대로, 조인 모델에서 'qty'를 통해 가져 오지만 실제 구성 요소는 가져 오지 않습니다.

나는 누군가가 놀 다운로드 할 수있는 공공의 repo를 넣어했습니다 https://github.com/EssentialMusic/Recipes

모델 :

class Ingredient < ActiveRecord::Base 
    attr_accessible :name 
    has_many :recipe_ingredients, :dependent => :destroy 
    has_many :recipes, :through => :recipe_ingredients 
end 

class Recipe < ActiveRecord::Base 
    attr_accessible :author, :description, :name, :recipe_ingredients_attributes, :ingredients_attributes 
    has_many :recipe_ingredients, :dependent => :destroy 
    has_many :ingredients, :through => :recipe_ingredients 
    accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true 
    accepts_nested_attributes_for :recipe_ingredients 
end 

class RecipeIngredient < ActiveRecord::Base 
    belongs_to :recipe 
    belongs_to :ingredient 
    attr_accessible :measure, :qty, :special_instructions 
end 

양식

<%= form_for(@recipe) do |f| %> 
    <% if @recipe.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2> 

     <ul> 
     <% @recipe.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :author %><br /> 
    <%= f.text_field :author %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %><br /> 
    <%= f.text_area :description %> 
    </div> 

<div> 
    <%= f.fields_for :recipe_ingredients do |ri| %> 
    <%= ri.text_field :qty %> - 
     <%= ri.fields_for :ingredients do |i| %> 
      <%= i.text_field :name %><br> 
     <% end %> 
    <% end %> 
</div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

건배를!

답변

0

대체이 같은 두 번째 중첩 된 fields_for에서 F와 리 :

<%= f.fields_for :recipe_ingredients do |ri| %> 
<%= ri.text_field :qty %> - 
     <%= **f**.fields_for :ingredients do |i| %> 
     <%= i.text_field :name %><br> 
     <% end %> 
<% end %>  
+0

가 하나 개의 성분, 그러나 더있을 경우 두 성분의 경우 .... 여분의 결과를 반환 할 것 같으면 잘 작동, 당신은 그것들을 각 라인에 대해 나열 해 줄 것입니다 .... 복제 되었습니까? 그래서 2x2라면 말이 되겠습니까? – Raoot