【问题标题】:Rails 5 strong parameters with nested multiple attributesRails 5 具有嵌套多个属性的强参数
【发布时间】:2017-01-09 02:54:07
【问题描述】:

我正在尝试保存一个包含配方和多种成分的对象。数据来自我的 angular 2 应用程序,它将对象作为 JSON 传递。我的 rails 5 api 应用程序将接收配方对象并使用强参数将其直接保存到我的数据库中。现在我可以将配方对象保存到数据库中,但由于某种原因,其中的成分没有被保存。我检查了 rails 文档,发现我当前的代码没有问题。任何想法将不胜感激。

食谱.rb

class Recipe < ApplicationRecord
 has_many :ingredients
 accepts_nested_attributes_for :ingredients
end

成分.rb

class Ingredient < ApplicationRecord
 belongs_to :recipe
end

recipe_controller.rb

def create
  @recipe = Recipe.new(recipe_params)
  if @recipe.save                                                                                                                                                 
    render json: @recipe, status: :created, location: @recipe
  else
    render json: @recipe.errors, status: :unprocessable_entity
  end
end

def recipe_params
  params.require(:recipe).permit(:name , :description, :imagePath, ingredients_attributes: [ :id, :name, :amount])
end

我的控制台日志

      Started POST "/recipes" for ::1 at 2017-01-09 11:40:44 +0900
      ActiveRecord::SchemaMigrationLoad(0.3ms)SELECT`schema_migrations`.* FROM `schema_migrations`

      Processing by RecipesController#create as HTML Parameters: {"name"=>"Spaghetti", "imagePath"=>"http://cdn2.tmbi.com/TOH/Images/Photos/37/300x300/exps36749_SD143203D10__25_1b.jpg", "description"=>"Delicious spaghetti", "ingredients"=>[{"name"=>"Tomato", "amount"=>1}, {"name"=>"Pasta", "amount"=>1}], "recipe"=>{"name"=>"Spaghetti", "description"=>"Delicious spaghetti", "imagePath"=>"http://cdn2.tmbi.com/TOH/Images/Photos/37/300x300/exps36749_SD143203D10__25_1b.jpg"}}


     (0.1ms)  BEGIN
      SQL (0.2ms)  INSERT INTO `recipes` (`name`, `description`, `imagePath`, `created_at`, `updated_at`) VALUES ('Spaghetti', 'Delicious spaghetti', 'http://cdn2.tmbi.com/TOH/Images/Photos/37/300x300/exps36749_SD143203D10__25_1b.jpg', '2017-01-09 02:40:44', '2017-01-09 02:40:44')
     (0.7ms)  COMMIT
     Completed 201 Created in 9ms (Views: 1.0ms | ActiveRecord: 2.1ms)

【问题讨论】:

标签: ruby-on-rails angularjs activerecord ruby-on-rails-5 strong-parameters


【解决方案1】:

在Rails 5 中,每当我们定义belongs_to 关联时,默认情况下需要在https://github.com/rails/rails/pull/18937change 之后存在关联记录。 如果你显示@recipe.errors.messages,你会发现

{:"ingredient.recipe"=>["must exist"]}

只需将 'optional:true' 添加到成分模型中的 belongs_to 行即可。

class Ingredient < ApplicationRecord
 belongs_to :recipe, optional: true
end

【讨论】:

  • 对不起,我不明白你的意思。我正在尝试将配方和其中的成分保存在我的配方控制器中。
  • 如果您有验证错误,它将不会保存。 Rails 5 要求在保存记录之前存在父关联,这一事实将产生验证错误,并且不允许控制器工作。如果你在 Rails 4 中尝试你的代码,它会工作,但不是 Rails 5。通过使用 optional:true,你可以在没有设置父 id 的情况下保存孩子。
【解决方案2】:

您的控制台日志显示参数包含密钥 ingredients。对于嵌套属性,您必须改用ingredients_attributes。所以确保你的参数看起来像这样:

{
  "name" => "Spaghetti", 
  "description" => "Delicious spaghetti", 
  "ingredient_attributes" => [
    {"name" => "Tomato", "amount" => 1}, 
    {"name" => "Pasta", "amount" => 1}
  ]
}

为什么我们不能在这里简单地使用ingredients?好吧,这将导致以下代码在内部执行:

recipe.ingredients = [{"name" => "Tomato", … }]

但recipe.ingredients= 是关系的属性写入者,它只接受Ingredient 实例的数组。所以你必须像这样使用它:

recipe.ingredients = [Ingredient.new("name" => "tomato", …)]

在底层,使用 recipe.ingredients_attributes= 本质上是从普通哈希转换为 Ingredient 的实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多