2

I'm new to rails (my 5th week) and currently I learn with the book "agile web dev with rails 4". I just finished chapter 11 and to my own amusement, I planed to add a remote call to destroy all quantity from a item in the cart. Well, the problem is, I get a internal error message 500.

I added the code "remote: true" in file line_items/_line_item.html.erb

...
...
<%= button_to 'Remove Item', line_item, method: :delete, remote: true %>

And changed the redirect for html to store_path and added a format.js in the line_items_controller.rb

I created a destroy.js.erb file and added the code

$('#cartContainer').html('<%= escape_javascript render(@cart) %>');

I guess the @cart is wrong. But I just can't figured out and probably i lost totally the overview over the hole project :)

Here the server log:

Started DELETE "/line_items/105" for 127.0.0.1 at 2014-08-07 12:44:15 +0200
Processing by LineItemsController#destroy as JS
Parameters: {"authenticity_token"=>"ofobuV7Pk1avFiV6KSQ7NHnc/J77PJWWejsKVyg3YLQ=", "id"=>"105"}
LineItem Load (0.1ms)  SELECT  "line_items".* FROM "line_items"  WHERE "line_items"."id" = ? LIMIT 1  [["id", 105]]
CACHE (0.0ms)  SELECT  "line_items".* FROM "line_items"  WHERE "line_items"."id" = ? LIMIT 1  [["id", "105"]]
(0.1ms)  begin transaction
SQL (0.4ms)  DELETE FROM "line_items" WHERE "line_items"."id" = ?  [["id", 105]]
(1.6ms)  commit transaction
Rendered line_items/destroy.js.erb (0.9ms)
Completed 500 Internal Server Error in 8ms

ActionView::Template::Error ('nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.):
1: // reload cart after adding item
2: $('#cartContainer').html('<%= escape_javascript render(@cart) %>');
app/views/line_items/destroy.js.erb:2:in `_app_views_line_items_destroy_js_erb__1371414078062466935_70243559055940'
app/controllers/line_items_controller.rb:81:in `destroy'

1 Answers1

2

You are getting this error because @cart is nil. In your controllers destroy method initialise @cart

def destroy
  @cart = current_cart
  // other logic
end
Mandeep
  • 9,093
  • 2
  • 26
  • 36
  • 1
    Thx you very much you helped me a lot. This was a quick and easy solution. And i see now, i forgot to add :destroy in the before_action method. – AdvanceInBeginning Aug 07 '14 at 11:29