【问题标题】:Rails 4.2.4 ActiveRecord::RecordNotFound in LineItemsController#createRails 4.2.4 ActiveRecord::RecordNotFound 在 LineItemsController#create
【发布时间】: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


    【解决方案1】:

    没有params[:product_id]

    使用这个

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

    【讨论】:

    • 抱歉,这里出现错误,这是错误消息:nil:class 的未定义方法“[]”。我有点坚持外键的概念,[:line_item] 是 nil 吗?
    【解决方案2】:

    错误可以在 line_items index.html.erb 视图中找到 app/views/index.html.erb 在第 34 行。

    你有一个小错字:

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

    应该是product_id: 而不是prodcut_id:

    该行应如下所示:

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

    【讨论】:

      【解决方案3】:

      Agile Web Development with Rails 6》一书也遇到了同样的问题, 此代码有效,line_items_controller:

          def create
      if params[:product_id]
        product = Product.find(params[:product_id])
        @line_item = @cart.add_product(product)
      else
        @line_item = @cart.line_items.build(line_item_params)
      end
      
      respond_to do |format|
        if @line_item.save
          format.html { redirect_to store_index_url, notice: "Line item was successfully created." }
          format.js { @current_item = @line_item }
          format.json { render :show, status: :created, location: @line_item }
        else
          format.html { render :new, status: :unprocessable_entity }
          format.json { render json: @line_item.errors, status: :unprocessable_entity }
        end
       end
      end
      

      及其系统测试用例:

         test "creating a Line item" do
          visit line_items_url
          click_on "New Line Item"
      
          fill_in "Cart", with: @line_item.cart_id
          fill_in "Product", with: @line_item.product_id
          click_on "Create Line item"
      
          assert_text "Line item was successfully created"
        end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-21
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多