【发布时间】:2015-12-15 17:15:57
【问题描述】:
我是 Rails 新手,正在学习使用 Rails 4 进行敏捷 Web 开发一书,并使用 Rails 4.2.4 版和 Ruby 2.1.5 版。我目前处于迭代 D3 中,但遇到以下错误:
ActiveRecord::RecordNotFound in LineItemsController#create
Couldn't find Product with 'id'=
Rails.root: c:/Ruby on Rails/Agile Rail Development/depot
Application Trace | Framework Trace | Full Trace<br>
app/controllers/line_items_controller.rb:29:in `create'
这是它图片的链接:Link to the error。
在这里,我将代码推送到了 GitHub:Link to the full code。
这是我的 line_items_controller.rb 文件。
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
def create
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 :show, status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
private
def set_line_item
@line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id, :cart_id)
end
end
希望能得到您的帮助,谢谢!!
【问题讨论】:
标签: ruby-on-rails-4