0

我正在学习 Rails,非常感谢有关创建具有多个同名输入字段的表单对象的建议。我遇到了未经许可的参数错误。找资料好几天了,试了很多,没有结果。这是我的第一个问题,如果不容易理解,请见谅。

我正在使用 rails 6.0.0,并且有 3 个模型:Recipe、Ingredient、RecipeIngredientRelation。我想为成分和食谱成分创建多个输入字段。错误:

Parameters: {"authenticity_token"=>"G6vwDdptMBgl2SPvLfS4BoiCjoRnf+FGYuKW0Ykt3Aodt5HfBEx5KHWBEpl4r52ANCv9QWz13rDyAP42qCyESg==", "recipes_ingredient"=>{"name"=>"Recipe", "portions"=>"1", "time_count_id"=>"3", "calories"=>"", "ingredients"=>{"i_name"=>"water"}, "recipe_ingredient_relations"=>{"quantity"=>["100"], "measurement_id"=>"3"}, "recipe"=>"some text", "tips"=>"", "is_public"=>"0"}, "commit"=>"レシピを投稿する"}
Unpermitted parameters: :ingredients, :recipe_ingredient_relations

binding.pry 让我知道@recipes_ingredient.errors:

@details={:i_name=>[{:error=>:blank}]},
@messages={:i_name=>["can't be blank"]}>

我的模型是: 食谱

has_many :recipe_ingredient_relations
has_many :ingredients, through: :recipe_ingredient_relations
accepts_nested_attributes_for :ingredients, :recipe_ingredient_relations, allow_destroy: true

成分

has_many :recipe_ingredient_relations
has_many :recipes,  through: :recipe_ingredient_relations

配方成分关系

belongs_to  :recipe
belongs_to  :ingredient

表单对象

class RecipesIngredient

  include ActiveModel::Model
  attr_accessor :ingredients, :i_name, :recipe_ingredient_relations, :quantity, :measurement_id, :name, :image, :portions, :time_count_id, :recipe, :tips, :calories, :is_public, :user_id

  with_options presence: true do
    validates :i_name #, uniqueness: true
    validates :quantity, length:{maximum: 10}, allow_blank: true
    validates :measurement_id, allow_blank: true
    validates :name
    validates :portions
    validates :time_count_id, numericality: {other_than: 1}
    validates :recipe
    validates :tips, allow_blank: true
    validates :calories, allow_blank: true
    validates :is_public
  end

  def save
    recipe = Recipe.create(user_id: user_id, name: name, image: image, portions: portions, time_count_id: time_count_id, recipe: recipe, tips: tips, calories: calories, is_public: is_public)
    ingredient = Ingredient.create(i_name: i_name)
    recipe_ingredient_relation = RecipeIngredientRelation.create(recipe_id: recipe.id, ingredient_id: ingredient.id, quantity: quantity, measurement_id: measurement_id)
  end
end

输入表单视图的一部分:

%= form_with(model: @recipes_ingredient, url:recipes_path, local: true) do |form| %>
<...>
<%= form.fields_for :ingredients do |f| %>

        <%= f.text_field :i_name,  placeholder: "例)レタス&quot;, class: "form-el ingredient-input required" %>
      <% end %>
      <%= form.fields_for :recipe_ingredient_relations do |f| %>
          <div class="quantity">
            <%= f.text_field :quantity, multiple: true, placeholder: "例)100", class: "form-el quantity-input required" %>
            <%= f.collection_select(:measurement_id, Measurement.all, :id, :name, {}, {class:"after-input-required"}) %>
            <p class="after-input after-input-btn-required">+</p>
          </div>
        </div>
      <% end %>

最后,Recipes 控制器:

class RecipesController < ApplicationController
  def index
    @recipe = Recipe.all
  end

  def new
    @recipes_ingredient = RecipesIngredient.new
  end

  def create
    @recipes_ingredient = RecipesIngredient.new(recipe_params)
binding.pry

   #   if @recipe.valid?
  #     @recipe.save
  #     redirect_to root_path
  #   else
  #     render action: :new
  #   end
  end

  private

  def recipe_params
    params.require(:recipes_ingredient).permit(:name, :image, :portions, :time_count_id, :recipe, :tips, :calories, :is_public, ingredients_attributes: [:id, :i_name], recipe_ingredient_relations_attributes: [:id, :quantity, :measurement_id] ).merge(user_id: current_user.id)
  end
end
4

0 回答 0