使用accepts_nested_attributes_for和fields_for创建控制器方法

时间:2018-05-25 13:01:20

标签: ruby-on-rails associations nested-attributes

我正在使用Rails 5.2.0构建一个关于食谱的Web应用程序,我对控制器的create方法有疑问。

这是我的模特:

class Recipe < ApplicationRecord
    belongs_to :user
    has_many :quantities
    has_many :ingredients, through: :quantities

    accepts_nested_attributes_for :quantities, allow_destroy: true
end

class Quantity < ApplicationRecord    
    belongs_to :recipe
    belongs_to :ingredient
end

class Ingredient < ApplicationRecord
    has_many :quantities
    has_many :recipes, through: :quantities
end

这里是创建新食谱的视图:

<%= form_for(@recipe) do |f| %>

    <%= f.label :name, "Name" %>
    <%= f.text_field :name %>

    <%= f.label :servings, "Servings" %>
    <%= f.number_field :servings %>


    <%= f.fields_for :quantities do |quantity| %>

        <%= f.hidden_field :_destroy, class: "hidden-field-to-destroy" %>

        <%= f.label :ingredient_id, "Ingredient Name" %>
        <%= f.text_field :ingredient_id%>

        <%= f.label :amount, "Amount" %>
        <%= f.number_field :amount %>

        <%= f.label :unit, "Unit" %>
        <%= f.select(:unit, ["kg","g","l","ml"], {include_blank: true}) %>
    <% end %>

    <%= f.submit 'Add new recipe' %>

<% end %>

我可以使用jquery动态添加新成分,并以相同的形式删除它们。

控制器的update方法工作正常,但create方法不起作用:

class RecipesController < ApplicationController
    def create
        @recipe = current_user.recipes.build(recipe_params)
        if @recipe.save
            flash[:success] = "New recipe created correctly."
            redirect_to @recipe
        else
          render 'new'
        end
    end 

    def update
        @recipe = Recipe.find(params[:id])
        if @recipe.update_attributes(recipe_params)
          flash[:success] = "The recipe has been updated correctly."
          redirect_to @recipe
        else
          render 'edit'
        end
    end

    private
        def recipe_params
            params.require(:recipe).permit( :name, :servings, quantities_attributes: [:ingredient_id, :amount, :unit,:_destroy, :id, :recipe_id])
        end
end

我正在尝试@recipe = current_user.recipes.build(recipe_params),但我在te视图中收到以下错误:

  • 数量配方不能为空白

我认为这是因为在尝试创建关系时,需要指示recipe_id,但尚未创建配方并且无法指示id。

您能告诉我一个人,首先创建配方的正确方法是什么,然后能够通过配方​​控制器的create方法中的Quantity添加成分?

1 个答案:

答案 0 :(得分:0)

根据共享的消息,qunatity_recipes不能为空,并且您没有指定任何条件来管理它。

当前

class Recipe < ApplicationRecord
 belongs_to :user
 has_many :quantities
 has_many :ingredients, through: :quantities

 accepts_nested_attributes_for :quantities, allow_destroy: true
end

将接受嵌套属性更新为Recipe类的allow_nil

class Recipe < ApplicationRecord
 belongs_to :user
 has_many :quantities
 has_many :ingredients, through: :quantities

 accepts_nested_attributes_for :quantities, allow_destroy: true, allow_nil: true
end