Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 用cocoon嵌套多个模型_Ruby On Rails_Activerecord_Ruby On Rails 5_Model Associations_Cocoon Gem - Fatal编程技术网

Ruby on rails 用cocoon嵌套多个模型

Ruby on rails 用cocoon嵌套多个模型,ruby-on-rails,activerecord,ruby-on-rails-5,model-associations,cocoon-gem,Ruby On Rails,Activerecord,Ruby On Rails 5,Model Associations,Cocoon Gem,我正在尝试创建嵌套表单,它将为两个关联模型组合两个属性 假设我有3个模型:配方主模型、配方和配料的RecipeCredit联接模型 代码如下: class Recipe < ApplicationRecord has_many :directions, inverse_of: :recipe has_many :recipe_ingredients, inverse_of: :recipe has_many :ingredients, through: :recip

我正在尝试创建嵌套表单,它将为两个关联模型组合两个属性

假设我有3个模型:配方主模型、配方和配料的RecipeCredit联接模型

代码如下:

class Recipe < ApplicationRecord
    has_many :directions, inverse_of: :recipe
    has_many :recipe_ingredients, inverse_of: :recipe
    has_many :ingredients, through: :recipe_ingredients

    accepts_nested_attributes_for :ingredients,
                                    reject_if: proc { |attributes| attributes['name'].blank? },
                                    allow_destroy: true
    accepts_nested_attributes_for :directions,
                                    reject_if: proc { |attributes| attributes['step'].blank? },
                                    allow_destroy: true
    accepts_nested_attributes_for :recipe_ingredients,
                                    reject_if: proc { |attributes| attributes['quantity'].blank? },
                                    allow_destroy: true


    validates :tittle, :description, :image, presence: true
    has_attached_file :image, styles: { :medium => "400x400#" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/

end
class Ingredient < ApplicationRecord
    has_many :recipe_ingredient, inverse_of: :ingredient
    has_many :recipes, through: :recipe_ingredient
end
class RecipeIngredient < ApplicationRecord
  belongs_to :recipe
  belongs_to :ingredient, inverse_of: :recipe_ingredient

end
在这里,您可以看到我正在尝试访问两个属性: 1.类接收元件的数量-此部分正常 2.类别成分的名称-此部分完全相同

.form-inline.clearfix
        .nested-fields
            = f.input :quantity, :label => "Ilość", input_html: { class: "form-input form-control" }
            = f.fields_for :ingredients_attributes do |ingr|
                = ingr.input :name, :label => "Nazwa", input_html: { class: "form-input form-control" }

            = link_to_remove_association "Usun", f, class: "form-button btn btn-default"
在验证过程中,我得到了这个错误

配方成分必须存在

原因如下:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"nw14a3HkgSrjE+QpIK4POEzTOqBhtE/EMe+CarWkmI6CSRf0GAdWZGzJQRD4aPurNDNm96TJbt60mc1hl+1JPA==", "recipe"=>{"tittle"=>"Tosty", "description"=>"Testowy przepis", "preparation_time"=>"10.0", "portion"=>"2", "recipe_ingredients_attributes"=>{"1507996685991"=>{"quantity"=>"432", "ingredients_attributes"=>{"name"=>"fasdd"}, "_destroy"=>"false"}, "1507996689888"=>{"quantity"=>"2134432342", "ingredients_attributes"=>{"name"=>"dsad"}, "_destroy"=>"false"}}, "directions_attributes"=>{"0"=>{"step"=>"Krok1", "_destroy"=>"false", "id"=>"1"}, "1"=>{"step"=>"Krok2", "_destroy"=>"false", "id"=>"2"}}}, "commit"=>"Update Recipe", "id"=>"5"}
  Recipe Load (0.3ms)  SELECT  "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT ?  [["id", 5], ["LIMIT", 1]]
Unpermitted parameter: ingredients_attributes
Unpermitted parameter: ingredients_attributes
   (0.2ms)  begin transaction
  Direction Load (0.3ms)  SELECT "directions".* FROM "directions" WHERE "directions"."recipe_id" = ? AND "directions"."id" IN (1, 2)  [["recipe_id", 5]]
   (0.2ms)  rollback transaction
传递给方法的参数与配方控制器中配方参数私有方法的定义不匹配:

def recipe_params
        params.require(:recipe).permit(:tittle, :description, :image, :portion, :preparation_time, ingredients_attributes: [:id, :name, :_destroy], directions_attributes: [:id, :step, :_destroy], recipe_ingredients_attributes: [:id, :quantity, :_destroy])
    end

关键是。。。如何修复?因此配料属性将从配方配料属性中传递出去?

日志文件中的错误表明不允许使用配料属性参数。如果您检查传递的参数,您可以看到recipe_components_属性包含components_属性,这是强参数定义recipe_params方法所不允许的:它还必须支持正确的嵌套

多亏了内森

答案可以直接在cocoon git中找到:

我知道问题出在哪里。关键是我不知道如何解决这个问题。在我使用成分模型中包含数量属性的嵌套表单之前。但对于功能,我必须添加连接模型,现在我不知道如何为成分:名称和RecipeIngCredit:quantity生成适当的关联控件
def recipe_params
        params.require(:recipe).permit(:tittle, :description, :image, :portion, :preparation_time, ingredients_attributes: [:id, :name, :_destroy], directions_attributes: [:id, :step, :_destroy], recipe_ingredients_attributes: [:id, :quantity, :_destroy])
    end