Rails 为什么我的父对象没有保存?

Rails why is my parent object not saving?

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

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

我试图在选择特定的 Meal 对象(使用 collection_check_box) 和关联的 成分 对象。

查看本地主机服务器日志,使用从正确 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

我的购物清单模型:

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_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

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

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

所以当你 运行

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

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

add_column :meals, :shopping_list_id, :integer