我正在关注第 4 版 Agile Web Dev with Rails。在第 9 章第 112 页。我正在尝试运行 rake test:functional。我想我遵循了他们的每一段代码,但这给了我一个质量分配错误。当我运行服务器时,它给了我这个错误
ActiveModel::MassAssignmentSecurity::LineItemsController#create 中的错误
无法批量分配受保护的属性:产品
这就是 LineItemsController 创建函数的样子
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(product: product)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart,
notice: 'Line item was successfully created.' }
format.json { render json: @line_item,
status: :created, location: @line_item }
else
format.html { render action: "new" }
format.json { render json: @line_item.errors,
status: :unprocessable_entity }
end
end
end
这是文件夹 test/functional/ 中 line_items_controller_test.rb 中的创建测试
test "should create line_item" do
assert_difference('LineItem.count') do
post :create, product_id: products(:ruby).id
end
assert_redirected_to cart_path(assigns(:line_item).cart)
end
我错过了什么?