0

我想創建嵌套的形式,將結合兩個相關模型的屬性。嵌套多個模型與繭

比方說,我有3個模型:配方(主模型),食譜配方(加入配方和配料模型)和配料。

代碼如下:

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 

我的嵌套:

.col-md-6 
       %h3 Skladniki 
       #ingredients 
        = f.simple_fields_for :recipe_ingredients do |recipe_ingredient| 
         =render 'recipe_ingredient_fields', f: recipe_ingredient 
        .links 
         = link_to_add_association 'Dodaj skladnik', f, :recipe_ingredients, class: "btn btn-default add-button", :wrap_object => Proc.new {|recipe_ingredient| recipe_ingredient.build_ingredient; recipe_ingredient } 

在這裏你可以看到看到,我試圖訪問兩個屬性: 1.數量(類RecipeIngredient的) - 這部分是好的 2.名稱(類成分) - 這部分是完整的KO

.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 

參數傳遞給方法,只是不recipe_params的比賽定義(私有方法recipe_controller):

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_attributes將被傳遞出recipe_ingredients_attributes?

回答

1

日誌文件中的錯誤指出ingredients_attributes參數不被允許。如果您檢查傳遞的參數,您可以看到recipe_ingredients_attributes包含ingredients_attributes,這是強參數定義(recipe_params方法)不允許的:它必須支持正確的嵌套。

+0

我知道問題出在哪裏。關鍵是我不知道如何解決這個問題。 之前我使用了包含在Ingredient模型中的數量屬性的嵌套表單。但是對於功能,我不得不添加連接模型,現在我不知道如何爲成分生成適當的assiociated控件:name and RecipeIngredient:quantity – MajQel