Reputation: 542
I am learning ruby from the book "Agile web development with rails, 4th edtion" . I am using rails 3.2 and ruby 1.9.2 .
I tried to make a call to the AJAX function of the card using the following code
<%= button_to 'Add to Cart', line_items_path(product_id: product) , remote: true %>
Then in the controller's "create" action method for line_item added
respond_to do |format|
if @line_item.save
# format.html { redirect_to @line_item.cart,notice: 'Line item was successfully created.' }
format.html { redirect_to store_url }
format.js
Then created the create.js.erb file in line_items view and added the content
$('#cart').html("<%=j render @cart %>");
The "cart" partial is
<div class="cart_title">Your Cart</div>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart', cart, method: :delete, confirm: 'Are you sure?' %>
The "line_item" partial is
<tr>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>
Now when I run this with a normal refresh of the page, the cart contents update and it works fine but when I do it using the "Add to cart" button shown above, it fires an AJAX call and it seems that somehow the control is not reaching the create.js.erb file or the "line_items" partial. When I saw in the development logs, I saw the following message.
Started POST "/line_items?product_id=1" for 127.0.0.1 at 2012-07-24 22:04:24 +0530
Processing by LineItemsController#create as JS
Parameters: {"authenticity_token"=>"+8jnhCPyLhj26JDuaP1LRUNhVGGlOpFyJcfVXoffX+U=", "product_id"=>"1"}
[1m[35mCart Load (0.2ms)[0m SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 14]]
[1m[36mProduct Load (0.1ms)[0m [1mSELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1[0m [["id", "1"]]
[1m[35mLineItem Load (0.3ms)[0m SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 14 AND "line_items"."product_id" = 1 LIMIT 1
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35m (0.8ms)[0m UPDATE "line_items" SET "quantity" = 18, "updated_at" = '2012-07-24 16:34:24.620944' WHERE "line_items"."id" = 40
[1m[36m (12.1ms)[0m [1mcommit transaction[0m
[1m[35mLineItem Load (0.2ms)[0m SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 14
[1m[36mProduct Load (0.2ms)[0m [1mSELECT "products".* FROM "products" WHERE "products"."id" = 1 LIMIT 1[0m
Rendered line_items/_line_item.html.erb (5.3ms)
Rendered carts/_cart.html.erb (7.2ms)
Rendered line_items/create.js.erb (8.4ms)
Completed 500 Internal Server Error in 30ms
ActionView::Template::Error (undefined method `title' for nil:NilClass):
1: <tr>
2:
3: <td><%= line_item.quantity %>×</td>
4: <td><%= line_item.product.title %></td>
5: <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
6: </tr>
7: <!-- <%= debug(line_item) %> -->
app/views/line_items/_line_item.html.erb:4:in `_app_views_line_items__line_item_html_erb__4463614436040447986_70300167607120'
app/views/carts/_cart.html.erb:3:in `_app_views_carts__cart_html_erb___214977825009850242_70300164957540'
app/views/line_items/create.js.erb:3:in `_app_views_line_items_create_js_erb___2733752682449914066_70300166787700'
app/controllers/line_items_controller.rb:52:in `create'
Please help in pin pointing the error ? Thanks
Upvotes: 0
Views: 853
Reputation: 19203
You are getting a line_item that does not have a product. So when you do line_item.product
you get a nil
and then, when you do nil.title
an error is thrown and so the view isn't rendered. Perhaps you forgot to fetch the (correct) line_item in the create action?
Upvotes: 2