Während ich bin, um neues Objekt des Klassenrezepts zu erstellen, werden keine Daten in Richtungsmodelltabelle eingefügt.Methode, die nicht in Tabelle schreibt
Rezept Controller:
class RecipesController < ApplicationController
before_action :find_recipe, only:[:show, :edit, :update, :destroy]
def index
@recipe = Recipe.all.order("created_at DESC")
end
def show
end
def new
@recipe = Recipe.new
end
def create
@recipe = Recipe.new(recipe_params)
if @recipe.save
redirect_to @recipe, notice: "Dodano nowy przepis"
else
render 'new'
end
end
def edit
end
def update
if @recipe.update(recipe_params)
redirect_to @recipe
else
render 'edit'
end
end
def destroy
@recipe.destroy
redirect_to root_path, notice: "Przepis został usunięty"
end
private
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
def find_recipe
@recipe = Recipe.find(params[:id])
end
end
Rezept Modell:
class Recipe < ApplicationRecord
has_many :directions, inverse_of: :recipe
has_many :recipe_ingredients
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
validates :tittle, :description, :image, presence: true
has_attached_file :image, styles: { :medium => "400x400#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
Richtung Modell:
class Direction < ApplicationRecord
belongs_to :recipe, inverse_of: :directions
end
Linie von der Konsole über neues Rezept-Objekt erstellen:
Started PATCH "/recipes/5" for 127.0.0.1 at 2017-10-13 14:26:06 +0200
Processing by RecipesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"h4Htdlybl6hiej2msXb5jlNz0Yngz3Mw2Trju9822FgPatUASvQx+it7m+p3WSBX9k7vjLOhJU3vhgtEFlvzIw==", "recipe"=>{"tittle"=>"Tosty", "description"=>"Testowy przepis", "preparation_time"=>"10.0", "portion"=>"2", "ingredients_attributes"=>{"0"=>{"name"=>"Chleb tostowy", "_destroy"=>"false", "id"=>"1"}, "1"=>{"name"=>"Ser zółty", "_destroy"=>"false", "id"=>"2"}, "2"=>{"name"=>"Cebula", "_destroy"=>"false", "id"=>"3"}}, "directions_attributes"=>{"1507897562367"=>{"step"=>"sdfsfd", "_destroy"=>"false"}, "1507897565151"=>{"step"=>"sdfsdfs", "_destroy"=>"false"}}}, "commit"=>"Update Recipe", "id"=>"5"}
Recipe Load (0.2ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
Unpermitted parameter: directions_attributes
(0.1ms) begin transaction
Ingredient Load (1.7ms) SELECT "ingredients".* FROM "ingredients" INNER JOIN "recipe_ingredients" ON "ingredients"."id" = "recipe_ingredients"."ingredient_id" WHERE "recipe_ingredients"."recipe_id" = ? AND "ingredients"."id" IN (1, 2, 3) [["recipe_id", 5]]
(0.5ms) commit transaction
Redirected to http://localhost:3000/recipes/5
Completed 302 Found in 21ms (ActiveRecord: 2.6ms)
Wie Sie oben in DB Query String sehen können, geht Richtung Attribute ohne ID und dies ist wahrscheinlich Hauptgrund. Allerdings ... Ich habe keine Ahnung, wie ich das beheben kann.
Bitte helfen Sie mir, wie ich mit diesem Problem seit einiger Zeit kämpfe.
Sie benötigen 'Bild', aber Sie bestehen es nicht. –