I'm trying to develop a cart system(for a practice e-commerce project) on rails 6.
The code(explaination):
I have a link with class attribute "add_to_cart_link" then I use a jquery to select the elements with the mentioned class and set a on click listener on them. The click listener send an ajax request to 'shopping_experience/add_to_cart'.The control then moves to "add_to_cart" method inside shoppingExperience controller.
the problem is that even though the add_to_cart method is executed, it doesn't update my session hash unless i use "binding.pry"(this is a feature of pry gem which is used for debugging). during pry whenever check the state of the hash(session hash) it's always what its suppose to be and changed when i add some thing to cart.
here is my view template(index.html.erb):
<p id="notice"><%= notice %></p>
<%= link_to 'view cart', display_cart_path %>
<h1>Items</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Category</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.category.title %></td>
<td><%= link_to 'Show', item %></td>
<td> <a class="add_to_cart_link" data-item_id=<%=item.id %> href="#"> add to cart button</a>
</td>
<td><%= link_to 'Edit', edit_item_path(item) %></td>
<td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
here is my custom (myjs.js.erb)
$( document ).ready(function() {
$(document).on('click', 'a.add_to_cart_link', function() {
console.log(this);
console.log($('meta[name="csrf-token"]').attr('content'));
var id = $(this).attr("data-item_id")
console.log(id)
$.ajax({
method: "POST",
headers: {'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')},
url: 'shopping_experience/add_to_cart',
data: { item_id: id },
})
});
});
add_to cart method:
def add_to_cart
if !session[:current_cart]
session[:current_cart] = {:shex_id => session[:current_cart_id] }
end
#checks if session hash's current cart key doesn't have params[:item_id] key, value pair
if !session[:current_cart].key?(params[:item_id])
# set {units: 1} as params[item_id] key's value
session[:current_cart][params[:item_id]] = {}
session[:current_cart][params[:item_id]][:quantity] = 1
else
#else increment value of quantity
session[:current_cart][params[:item_id]]["quantity"] = session[:current_cart][params[:item_id]]["quantity"] + 1
#ShexItem.create(item_id: params[:item_id], shopping_experience_id: session[:current_cart_id] )
end
binding.pry
puts(' at to cart method end')
end
so if remove the the bindin.pry from the end my session has isn't changed at all and i do get a "at the end of add to cart method" message in my terminal(even when i remove the binding.pry). this means that the add to card method is indeed called but my session doesn't get updated and if use pry to debug whats wrong it starts to work. I feel it might be related using an ajax request because maybe the session doesnt get updated or refreshed . i really am struggling with this can anyone help?
here is the github repo: https://github.com/HaroonAzhar/ship-shop (i think the repo is public),
if anyone wants to look up the code more.
So I got it to work. I saw a line on my terminal right after the "at the end of at to cart method" message, which said :
so I don't really know what head :no_content is but i thought it might have un-do/save changes made to session by my method. so at the end of add_to_cart method i redirected to index page with
still quite not sure why template not being found un-do my changes to hash, if anyone could share some light over this in the comments that'll be great! :)