【发布时间】: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
【问题讨论】:
-
您正在尝试一次构建多个项目并保存它们。尝试通过调用 shopping_list 关联来遍历您计划保存的所有项目,这个问题应该会消失。
-
谢谢@bkunzi01!不过,我不确定我是否理解您所说的“...调用 shopping_list 关联”是什么意思,您能举个例子吗?
标签: ruby-on-rails ruby strong-parameters model-associations