嵌套表单不显示

Nested form not showing

我的 _form 中有三个嵌套表单;成分、说明和营养。前两个工作正常,但营养不会显示任何形式。如果我在表单显示的视图中将 "nutrition" 重命名为其他名称,但由于它是错误的名称,所以参数无法正确发送,因此没有任何效果。我觉得这是我忽略的小事,但希望得到任何人的意见。

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>
class RecipesController < ApplicationController

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

字段:

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

您必须在控制器中建立关联才能默认显示 has_one fields_for。