4

私は Ruby on Rails の初心者で、すぐに Rails 4 から始めました。

Recipe と Ingredient モデルを正常にネストして、同じフォームに追加できるようにしました。次に、材料内に数量を入れ子にして、同じフォーム内に追加できるようにします。材料の量がデータベースに挿入されるまで、すべてが正常に機能しているように見えます。このことから、recipes_controller の強力なパラメーターに何か問題があると思います。しかし、以下に完全なコードを掲載します。フォームには simple_form を使用しています。

助けてくれてありがとう!

ここに私のモデルがあります:

class Recipe < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  has_many :ingredients, dependent: :destroy
  accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
  validates :title, presence: true
  validates :desc, presence: true
end


class Ingredient < ActiveRecord::Base
  belongs_to :recipe
  has_many :quantities, dependent: :destroy
  accepts_nested_attributes_for :quantities, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

class Quantity < ActiveRecord::Base
  belongs_to :ingredient
end

これがrecipes_controllerです

class RecipesController < ApplicationController

  def new
    @recipe = Recipe.new
    3.times do 
      ingredient = @recipe.ingredients.build
      1.times {ingredient.quantities.build }
    end
  end

  def create
    @recipe = Recipe.new(params[:recipe].permit(:title, :desc, ingredients_attributes: [:id, :recipe_id, :name, :_destroy, quantities_attributes: [:id, :ingredient_id, :amount, :unit, :_destroy]]))

    if @recipe.save
      redirect_to @recipe
    else
      render "new"
    end
  end

  def show
    @recipe = Recipe.find(params[:id])
  end


  def edit
    @recipe = Recipe.find(params[:id])
  end


  def update
    @recipe = Recipe.find(params[:id])

    if @recipe.update(params[:recipe].permit(:title, :desc))
      redirect_to @recipe
    else
      render 'edit'
    end
  end


  def destroy
    @recipe = Recipe.find(params[:id])
    @recipe.destroy

    redirect_to recipes_path
  end





  def index
    @recipes = Recipe.all
  end

  private
    def post_params
      params.require(:recipe).permit(:title, :desc, ingredients_attributes: [:id, :recipe_id, :name, :_destroy, quantities_attributes: [:id, :ingredient_id, :amount, :unit, :_destroy]])
    end
end

次に、単純なフォームを使用して、パーシャルを介してレシピ、材料、および量のフォームを作成します。

_形:

<%= simple_form_for @recipe do |f| %>
    <%= f.error_notification %>


    <%= f.input :title %>
    <%= f.input :desc %>



    <%= f.simple_fields_for :ingredients do |builder| %>

    <%= render "ingredient_fields", :f => builder %>


    <% end %>

    <p class="links">
    <%= link_to_add_association 'add ingredient', f, :ingredients %>
    <p class="links">
    <%= f.error :base%>
    <%= f.submit %>

<% end %>

_ingredients_fields からレンダリングされるもの:

<div class="nested-fields">
    <%= f.input :name, label: "Ingredient" %>



    <%= f.simple_fields_for :quantities do |builder| %>

    <%= render "quantities_fields", :f => builder %>

    <% end %>

    <%= link_to_remove_association "remove", f %>
</div>

_quantities_fields からレンダリングするもの: [編集済み]

<div class="nested-fields">
    <%= f.input :amount %>
    <%= f.input :unit %>
</div>

新しいレシピを追加しようとすると、次のログ ステートメントが生成されます。

Started POST "/recipes" for 127.0.0.1 at 2013-10-29 14:15:40 +0100
Processing by RecipesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"t6LKgDLwAxaU9xo2ipyCM+j1yfVF9WrI8AoGTX+gRkw=", "recipe"=>{"title"=>"Pancakes", "desc"=>"Tasty", "ingredients_attributes"=>{"0"=>{"name"=>"Milk", "quantities_attributes"=>{"0"=>{"amount"=>"1", "unit"=>"Cup"}}, "_destroy"=>"false"}}}, "commit"=>"Create Recipe"}
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36mSQL (3.5ms)[0m  [1mINSERT INTO "recipes" ("created_at", "desc", "title", "updated_at") VALUES (?, ?, ?, ?)[0m  [["created_at", Tue, 29 Oct 2013 13:15:40 UTC +00:00], ["desc", "Tasty"], ["title", "Pancakes"], ["updated_at", Tue, 29 Oct 2013 13:15:40 UTC +00:00]]
  [1m[35mSQL (0.4ms)[0m  INSERT INTO "ingredients" ("created_at", "name", "recipe_id", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Tue, 29 Oct 2013 13:15:40 UTC +00:00], ["name", "Milk"], ["recipe_id", 27], ["updated_at", Tue, 29 Oct 2013 13:15:40 UTC +00:00]]
  [1m[36m (7.8ms)[0m  [1mcommit transaction[0m
Redirected to http://www.projectcookbook.dev/recipes/27
Completed 302 Found in 22ms (ActiveRecord: 11.7ms)
4

2 に答える 2

0

I thing you are missing in this part

<%= simple_form_for @recipe do |f| %>

it should be

<%= simple_nested_form_for @recipe do |f| %>

于 2014-10-27T06:13:41.560 に答える