Maximilian Huang
Maximilian Huang

Reputation: 21

Rails 4.2.4 ActiveRecord::RecordNotFound in LineItemsController#create

I'm new to Rails, learning on the book Agile Web Development with Rails 4, and using the Rails version 4.2.4 with Ruby version 2.1.5 . I'm currently at the iteration D3 and I got stuck with an error stated below :

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'

Here is the link to the image of it : Link to the error.

And here I pushed the code to the GitHub : Link to the full code.

This is my line_items_controller.rb file.

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

It would be appreciate to have your help, thank you!!

Upvotes: 2

Views: 257

Answers (3)

Goaul
Goaul

Reputation: 1570

The same problem encountered also with book "Agile Web Development with Rails 6" and this code worked, 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

and system test case for it:

   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

Upvotes: 0

Klumbe
Klumbe

Reputation: 380

The error can be found in the line_items index.html.erb view at app/views/index.html.erb at line 34.

You have a little typo in there:

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

It should be product_id: instead of prodcut_id:.

The line should look like:

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

Upvotes: 0

SteveTurczyn
SteveTurczyn

Reputation: 36880

There is no params[:product_id]

Use this

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

Upvotes: 1

Related Questions