0
當我創建類別配方的新對象時,沒有數據被插入到方向模型表格中。不寫入表格的方法
配方控制器:
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
配方型號:
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
方向模型:從控制檯
class Direction < ApplicationRecord
belongs_to :recipe, inverse_of: :directions
end
線有關創建新配方對象:
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)
正如你可以在上面的db查詢字符串中看到的,方向屬性不帶id,這可能是主要原因。 Altough ...我不知道如何解決這個問題。
請幫忙,因爲我在這個問題上奮鬥了很長一段時間。
你需要'圖像',但你不通過它。 –