Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 Rails 4.2.4 ActiveRecord::在LineItemsController#create中找不到RecordNot_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 Rails 4.2.4 ActiveRecord::在LineItemsController#create中找不到RecordNot

Ruby on rails 4 Rails 4.2.4 ActiveRecord::在LineItemsController#create中找不到RecordNot,ruby-on-rails-4,Ruby On Rails 4,我是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 Trac

我是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'
ActiveRecord::RecordNotFound in LineItemsController#create
找不到具有“id”的产品=
root:c:/Ruby-on-Rails/Agile-Rail-Development/depot
应用程序跟踪|框架跟踪|完整跟踪
app/controllers/line\u items\u controller.rb:29:in'create'
以下是它的图像链接:

在这里,我将代码推送到GitHub:。

这是我的line\u items\u 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
class LineItemsController

非常感谢您的帮助,谢谢

没有
参数[:product\u id]

用这个

product = Product.find(params[:line_item][:product_id])

没有
参数[:product\u id]

用这个

product = Product.find(params[:line_item][:product_id])

该错误可在以下位置的行\u items
index.html.erb
视图中找到: app/views/index.html.erb,第34行

你有点输入错误:

<%= button_to 'Add to Cart', line_items_path(prodcut_id: product) %>

该错误可在以下位置的行\u items
index.html.erb
视图中找到: app/views/index.html.erb,第34行

你有点输入错误:

<%= button_to 'Add to Cart', line_items_path(prodcut_id: product) %>

抱歉,我在这里遇到了错误,这是错误消息:nil:class的未定义方法“[]”。我有点像被外键的概念卡住了,[:line_item]是nil吗?抱歉,但我在这里得到了错误,这是错误消息:nil:class的未定义方法“[]”。我有点被外键的概念所困扰,[:line_item]是零吗?