【问题标题】:Rails why is my parent object not saving?Rails 为什么我的父对象没有保存?
【发布时间】:2017-04-05 11:12:47
【问题描述】:

我对 rails/programming/web 开发很陌生,这是我得到的错误:

ActiveModel::MissingAttributeError(无法写入未知属性'shopping_list_id'

在选择特定的 Meal 对象(带有 collection_check_box)和相关的 Ingredient 个对象。

查看本地主机服务器日志,所有项目都是使用从正确的 meal_ids 检索到的正确成分(并分配给当前购物清单 ID)正确创建的 - 但是在保存实际购物清单时,我得到了上面错误。请参阅下面的服务器日志图像。

我已经被这个问题困扰了几天了,我看到很多帖子都有相同的错误,但似乎都对这个问题有完全不同的背景。

跟strong_parameters有关系吗?还是我的联想错了?

在我的饭菜中 index.html.erb:

<%= form_for :shopping_list, url: shopping_lists_path do |f| %>
  <%= collection_check_boxes :shopping_list, :meal_ids, Meal.all, :id, :name  do |b|%>
    <%= b.label class:"label-checkbox" do%>
       <%=b.check_box + b.text%>
    <%end%>
  <% end %>

  <%= f.submit "Create Shopping List" %>
<% end %>

我的 shopping_lists_controller:

def create

  @shopping_list = ShoppingList.new(shopping_list_params)

  @meals = Meal.where(id:[@shopping_list.meal_ids])
  @meals.map do |meal|
    meal.ingredients.map do |ingredient|
      @shopping_list.items.build(name: ingredient.name, quantity: ingredient.quantity, unit: ingredient.unit, shopping_list_id: @shopping_list.id)
    end
  end


  respond_to do |format|
    if @shopping_list.save
      format.html { redirect_to @shopping_list, notice: 'Shopping List was successfully created.' }
      format.json { render :show, status: :created, location: @shopping_list }
    else
      format.html { render :new }
      format.json { render json: @shopping_list.errors, status: :unprocessable_entity }
    end
  end 

end

私人

def set_shopping_list
  @shopping_list = ShoppingList.find(params[:id])
end


def shopping_list_params
  params.require(:shopping_list).permit(:name, {meal_ids: []})
end

我的 ShoppingList 模型:

class ShoppingList < ApplicationRecord
  has_many :items
  has_many :meals
  accepts_nested_attributes_for :meals
  accepts_nested_attributes_for :items
end

我的物品模型:

class Item < ApplicationRecord
  belongs_to :shopping_list
end

我的 schema.rb:

create_table "ingredients", force: :cascade do |t|
    t.integer  "meal_id"
    t.string   "name"
    t.float    "quantity"
    t.integer  "unit"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["meal_id"], name: "index_ingredients_on_meal_id"
  end

  create_table "items", force: :cascade do |t|
    t.integer  "shopping_list_id"
    t.string   "name"
    t.float    "quantity"
    t.integer  "unit"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.index ["shopping_list_id"], name: "index_items_on_shopping_list_id"
  end

  create_table "meals", force: :cascade do |t|
    t.string   "name"
    t.string   "description"
    t.string   "method"
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
    t.string   "meal_image_file_name"
    t.string   "meal_image_content_type"
    t.integer  "meal_image_file_size"
    t.datetime "meal_image_updated_at"
    t.integer  "diet"
  end

  create_table "shopping_lists", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

Image of server log

【问题讨论】:

  • 您正在尝试一次构建多个项目并保存它们。尝试通过调用 shopping_list 关联来遍历您计划保存的所有项目,这个问题应该会消失。
  • 谢谢@bkunzi01!不过,我不确定我是否理解您所说的“...调用 shopping_list 关联”是什么意思,您能举个例子吗?

标签: ruby-on-rails ruby strong-parameters model-associations


【解决方案1】:

问题是Meal 模型有belongs_to :shopping_list 但它没有列shopping_list_id

所以当你运行时

ShoppingList.create(meal_ids: ['1'])

Rails 尝试创建Meal 模型并将其链接回ShoppingList,但它不能,因为没有这样的属性并且错误告诉您ActiveModel::MissingAttributeError。修复创建迁移,例如:

add_column :meals, :shopping_list_id, :integer

【讨论】:

    【解决方案2】:

    shopping_lists_controllercreate 方法中,您正在使用shopping_list 的新实例并使用该实例的ID 创建items。由于新实例没有保存到数据库中,所以它不会有id。所以你不能在创建项目时使用@shopping_list.id

    所以要么改变

    @shopping_list = ShoppingList.new(shopping_list_params)
    

    @shopping_list = ShoppingList.create(shopping_list_params)
    

    或者做

    @shopping_list = ShoppingList.new(shopping_list_params)
    @shopping_list.save
    

    如果引发任何异常,您可能还想销毁此对象。

    【讨论】:

    • 感谢@Sajin,但我在尝试这两种方法时仍然遇到同样的错误。如果您查看服务器日志图像,“@shopping_list”对象的 id 已成功添加到每个“@item”对象的 shopping_list_id,但是当“@shopping_list”对象尝试保存到数据库时会出现问题。当我尝试 '@shopping_list = ShoppingList.create(shopping_list_params)' 解决方案时,错误出现在那里并且没有机会构建项目。
    • 我再次检查了日志,这些项目肯定保存成功,并且每个 item 对象都分配了正确的 shopping_list_id,但是 shopping_list 对象本身不会保存,也没有id。这让我很困惑!
    猜你喜欢
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 2011-11-14
    • 1970-01-01
    相关资源
    最近更新 更多