In this code:
<
% @products.each do |product| %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<p><%= sanitize(product.description) %></p>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
<!-- START_HIGHLIGHT -->
<%= button_to 'Add to Cart', line_items_path(product_id: product),
remote: true %>
<!-- END_HIGHLIGHT -->
</div>
</div>
<% end %>
At the bottom where we have button_to method with the params passed to it:
<%= button_to 'Add to Cart', line_items_path(product_id: product),
remote: true %>
Looking at the code, here is my understanding of what is going on, Did I understand it correctly or I have missed something?
In that code we are creating a button with text of “Add to Cart” and then we are
passing the product_id of the prodcut varaible to it, which is coming
from that for-each loop at the top and telling it go to link_items page with a
POST method for a product with ID of product_id .
Another question: in the lines_items_path could we just pass product.product_id ? or that wouldn’t work?
I think it is an example from the Agile Web Development with Rails book. I read it long time ago… if I remember it right, there are
products,ordersandline_items.if you call
rake routesyou will se thatline_items_path, as POST, is the route for thecreateaction in theline_items_controller. More details: CRUD verbs and actionsSo, you are passing the
product_idto the create action, where you should have something like: