NoMethodError in CartsController#destroy - Agile Web Development with Rails 4

≡放荡痞女 提交于 2019-12-02 02:39:27

问题


I'm learning Rails following the book Agile Web Development with Rails 4 and I got stuck with this Error:

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

This is the action Destroy of the Cart controller. The error info references to the line @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

This action is called from the "Empty car" button in the views/carts/shown.html.erb

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

Everything was working fine, but after creating a migration that added the column price to the table LineItems and modifying the method add_product from Carts for handling this new column, the destroy action is not working anymore.

this is 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

This is models/line_item.rb:

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

  def total_price
    price * quantity
  end
end

And this is the action create from line_items_controller that is used for adding products to the Cart:

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

Before this action, the method set_cart from the module CurrentCart is called:

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

When I run the controllers tests there are no failures. This is the test for the action destroy from Cart:

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

I can't see what is the cause of this error and I would like your help.

edit

This is the output from the server:

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'

Thanks :)


回答1:


Tried from rails console and got this:

2.2.0 :001 > carro = Cart.find(25)
  Cart Load (12.9ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 25]]
 => #<Cart id: 25, created_at: "2015-02-17 01:10:49", updated_at: "2015-02-17 01:10:49"> 
2.2.0 :002 > carro.destroy
   (0.2ms)  begin transaction
/home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:75: warning: circular argument reference - reflection
/home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:79: warning: circular argument reference - reflection
/home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:83: warning: circular argument reference - reflection
/home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:102: warning: circular argument reference - reflection
  LineItem Load (0.2ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ?  [["cart_id", 25]]
  SQL (0.3ms)  DELETE FROM "line_items" WHERE "line_items"."id" = ?  [["id", 116]]
  SQL (0.1ms)  DELETE FROM "line_items" WHERE "line_items"."id" = ?  [["id", 117]]
  SQL (0.1ms)  DELETE FROM "line_items" WHERE "line_items"."id" = ?  [["id", 118]]
   (0.2ms)  rollback transaction
NoMethodError: undefined method `name' for nil:NilClass
    from /home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:80:in `cached_counter_attribute_name'

What happens is that /home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:75: warning: circular argument reference - reflection is caused for a bug in activerecord according to this GitHub issue.

What I did was update the rails version. I was using rails 4.0.0, after updating to rails 4.1.2 the problem was solved.



来源:https://stackoverflow.com/questions/28545975/nomethoderror-in-cartscontrollerdestroy-agile-web-development-with-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!