虽然我在托盘中创建了类配方的新对象,但没有数据插入到方向模型表中。
食谱控制器:
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
来自控制台的关于创建新Recipe对象的行:
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 ...我不知道如何解决这个问题。
请帮助我解决这个问题很长一段时间。
答案 0 :(得分:0)
这是因为Unpermitted parameter: directions_attributes
。
directions__attributes
删除强力参数中的额外_
。
内部
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
更改为directions_attributes
。它会起作用。