2015-01-07 4 views
1

내 _form에 세 개의 중첩 된 양식이 있습니다. 재료, 지침 및 영양. 처음 두 가지는 잘 작동하지만 영양은 어떤 형태도 나타내지 않습니다. 보기에서 "영양"을 다른 이름으로 바꾸면 형식이 표시되지만 잘못된 이름이므로 매개 변수가 올바르게 전송되지 않으므로 아무 것도 작동하지 않습니다. 나는 그것이 내가 간과하고있는 작은 것이지만 anyones 입력을 얻고 싶다고 느낀다.중첩 된 양식이 보이지 않음

recipe.rb 중요한 비트

class Recipe < ActiveRecord::Base 


    has_many :ingredients, dependent: :destroy 
    validates :ingredients, presence: true 

    has_many :instructions, dependent: :destroy 
    validates :instructions, presence: true 

    accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true 
    accepts_nested_attributes_for :instructions, reject_if: :all_blank, allow_destroy: true 

    has_one :nutrition, dependent: :destroy 
    accepts_nested_attributes_for :nutrition 

end 

nutrition.rb

class Nutrition < ActiveRecord::Base 
    belongs_to :recipe 
end 

ingredient.rb 비교 nutrition.rb하는

class Ingredient < ActiveRecord::Base 
    belongs_to :recipe 

    validates :name, presence: true 
end 
recipes_controller.rb

의 관련 부분

class RecipesController < ApplicationController 

    # GET /recipes/new 
    def new 
    @recipe = Recipe.new 
    end 

    # POST /recipes 
    def create 
    @recipe = current_user.recipes.new(recipe_params) 

    if @recipe.save 
     @recipe.upvote_by current_user 
     redirect_to @recipe, notice: 'Recipe was successfully created.' 
    else 
     render :new 
    end 
    end 

    # PATCH/PUT /recipes/1 
    def update 
    if @recipe.update(recipe_params) 
     redirect_to @recipe, notice: 'Recipe was successfully updated.' 
    else 
     render :edit 
    end 
    end 

    private 
    # Never trust parameters from the scary internet, only allow the white list through. 
    def recipe_params 
     params.require(:recipe).permit(:name, :description, :image, :url, :category_id, ingredients_attributes: [:id, :name, :_destroy], instructions_attributes: [:id, :body, :image, :_destroy], nutrition_attributes: [:id, :serves, :serving_size, :calories, :fat, :carbs, :fiber, :protein]) 
    end 
end 

_form.html.erb

<%= form_for @recipe, html: { multipart: true } do |f| %> 
<h3>Nutrition</h3> 
    <div class="nutrition"> 
    <%= f.fields_for :nutrition do |n| %> 
<%# if I rename :nutrition to anything else the fields show, it seems to not enter this at all with :nutrition %> 
     <%= render 'nutrition_fields', f: n %> 
    <% end %> 
    </div> 

    <h3>Ingredients</h3> 
    <div class="ingredients"> 
    <%= f.fields_for :ingredients do |ingredient| %> 
     <%= render 'ingredient_fields', f: ingredient %> 
    <% end %> 
    <div class="links"> 
     <%= link_to_add_association 'add ingredient', f, :ingredients %> 
    </div> 
    </div> 

    <h3>Instructions</h3> 
    <div class="instructions"> 
    <%= f.fields_for :instructions do |instruction| %> 
     <%= render 'instruction_fields', f: instruction %> 
    <% end %> 
    <div class="links"> 
     <%= link_to_add_association 'add step', f, :instructions %> 
    </div> 
    </div> 

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

_nutrition_fields.html.erb

<div class="nested-fields"> 
    <div class="field"> 
    <%= f.label "Serves" %><br> 
    <%= f.text_field :serves %> 
    </div> 

    <div class="field"> 
    <%= f.label "Serving Size" %><br> 
    <%= f.text_field :serving_size %> 
    </div> 

    <div class="field"> 
    <%= f.label "Calories" %><br> 
    <%= f.text_field :calories %> 
    </div> 

    <div class="field"> 
    <%= f.label "Fat" %><br> 
    <%= f.text_field :fat %> 
    </div> 

    <div class="field"> 
    <%= f.label "Total Carbohydrates" %><br> 
    <%= f.text_field :carbs %> 
    </div> 

    <div class="field"> 
    <%= f.label "Fiber" %><br> 
    <%= f.text_field :fiber %> 
    </div> 

    <div class="field"> 
    <%= f.label "Protein" %><br> 
    <%= f.text_field :protein %> 
    </div> 
</div> 
+0

'f.fields_for f.object.build_nutrition do | n |'은 어떻습니까? – MrYoshiji

+0

@MrYoshiji 이제 필드 값이 0으로 표시됩니다. 그러나 제출하면 매개 변수를 "nutrition_attributes"로 보내지 않고 단지 "영양"이됩니다 – Jordan

+0

이상합니다. 그래서'f.fields_for : nutrition do | n |'을 사용하면 영양 분야가 사라지게됩니까? – MrYoshiji

답변

1
class RecipesController < ApplicationController 

# GET /recipes/new 
def new 
    @recipe = Recipe.new 
    @nutrition = @recipe.build_nutrition 
end 

필드에 대한 :

<%= f.fields_for :nutrition, @nutrition do |n| %> 

당신은 기본적으로 표시하는 fields_for has_one의 컨트롤러에 연결을 구축해야합니다.

+0

이것은 작동합니다. 또한 @@ recipe.build_nutrition'을 호출하면 괜찮은 것처럼 보일 수 있습니다. – Jordan

+0

FYI'f.object'는 폼에 채워진 객체를 반환합니다.'f.object'는'@ recipe'와 같습니다. – MrYoshiji