NoMethodError в CartsController # destroy - гибкая веб-разработка с Rails 4

Я изучаю Rails по книге Agile Web Development с Rails 4, и я застрял с этой ошибкой:

NoMethodError in CartsController#destroy
undefined method `name' for nil:NilClass

Это действие Destroy контроллера корзины. Информация об ошибке ссылается на строку@cart.destroy if @cart.id == session[:cart_id]:

# DELETE /carts/1
# DELETE /carts/1.json
def destroy
  @cart.destroy if @cart.id == session[:cart_id]
  session[:cart_id] = nil
  respond_to do |format|
    format.html { redirect_to store_url,
     notice: 'Your cart is currently empty'}
    format.json { head :no_content }
  end
end

Это действие вызывается кнопкой «Пустой автомобиль» вviews/carts/shown.html.erb

<%= button_to 'Empty cart', @cart, method: :delete, data: {confirm: 'Are you sure?'} %>

Все работало нормально, но после создания миграции добавили столбецprice к столуLineItems и модифицируя методadd_product отCarts для обработки этого нового столбца действие уничтожения больше не работает.

этоmodels/cart.rb

class Cart < ActiveRecord::Base
  has_many :line_items, dependent: :destroy

  def add_product(product_id, product_price)
    current_item = line_items.find_by(product_id: product_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(product_id: product_id, price: product_price)
    end
    current_item
  end

  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end

end

Этоmodels/line_item.rb:

class LineItem < ActiveRecord::Base
  belongs_to :product
  belongs_to :cart

  def total_price
    price * quantity
  end
end

И это действиеcreate отline_items_controller который используется для добавления товаров в корзину:

before_action :set_cart, only: [:create]

...

# POST /line_items
# POST /line_items.json
def create
  session[:counter] = 0
  product = Product.find(params[:product_id])
  @line_item = @cart.add_product(product.id, product.price)

  respond_to do |format|
    if @line_item.save
      format.html { redirect_to @line_item.cart }
      format.json { render action: 'show', status: :created, location: @line_item }
    else
      format.html { render action: 'new' }
      format.json { render json: @line_item.errors, status: :unprocessable_entity }
    end
  end
end

До этого действия методset_cart из модуляCurrentCart называется:

def set_cart
  @cart = Cart.find(session[:cart_id])
  rescue ActiveRecord::RecordNotFound
  @cart = Cart.create
  session[:cart_id] = @cart.id
end

Когда я запускаю тесты контроллеров, сбоев нет. Это тест на действиеdestroy из корзины:

test "should destroy cart" do
  assert_difference('Cart.count', -1) do
    session[:cart_id] = @cart.id
    delete :destroy, id: @cart
  end

  assert_redirected_to store_path
end

Я не понимаю, в чем причина этой ошибки, и мне нужна ваша помощь.

редактировать

Это вывод с сервера:

Started DELETE "/carts/15" for 127.0.0.1 at 2015-02-16 12:23:43 -0500
Processing by CartsController#destroy as HTML
  Parameters: {"authenticity_token"=>"hLsBQ0fR5b9M2lfxMc5McU1+dqtnZ2OKABRZV79vdHo=", "id"=>"15"}
  Cart Load (0.2ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", "15"]]
   (0.2ms)  begin transaction
  LineItem Load (0.9ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ?  [["cart_id", 15]]
  SQL (0.5ms)  DELETE FROM "line_items" WHERE "line_items"."id" = ?  [["id", 91]]
   (0.2ms)  rollback transaction
Completed 500 Internal Server Error in 73ms

NoMethodError (undefined method `name' for nil:NilClass):
  app/controllers/carts_controller.rb:58:in `destroy'

Спасибо :)

Ответы на вопрос(1)

Ваш ответ на вопрос