Skip to content

Commit 3e83309

Browse files
committed
Add To Basket without reloading the page
1 parent d63d61d commit 3e83309

7 files changed

Lines changed: 43 additions & 22 deletions

File tree

app/assets/javascripts/application.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@
1818
//= require bootstrap
1919
//= require jquery.nicescroll
2020

21-
// 1. Simple mode, it styles document scrollbar:
21+
// $(".add-to-basket").click(function(){
22+
// var productId = $(this).attr("productID");
23+
// $.post('/line_items_controller', {id: productId}).done(function(response){
24+
// if (response.status == "created"){
25+
// $('total_cart_items_badge').text(response.line_items_count);
26+
// }
27+
// else{
28+
// //your error handler
29+
// }
30+
// });
31+
// });

app/controllers/line_items_controller.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ class LineItemsController < ApplicationController
22
before_action :logged_in_user, only: %i[create destroy add_quantity reduce_quantity]
33

44
def create
5-
65
# Find associated product and current cart
76
chosen_product = Product.find(params[:product_id])
87
current_cart = @current_cart
@@ -20,13 +19,19 @@ def create
2019
# @line_item.order = Order.first
2120
@line_item.quantity = 1
2221
end
23-
24-
# Save and redirect to cart show path
25-
@line_item.save!
26-
22+
2723
# redirect_to cart_path(@current_cart)
2824
# redirect_back(fallback_location: root_url)
29-
# render layout: false
25+
26+
respond_to do |format|
27+
if @line_item.save!
28+
format.js
29+
# below is a second way without creating ``
30+
# format.js { render :js => "alert('hi')" }
31+
else
32+
format.html { render :new , notice: 'Error adding product to basket!' }
33+
end
34+
end
3035
end
3136

3237
def destroy

app/views/layouts/_header.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<!-- <i class="glyphicon glyphicon-user"></i> -->
3636
<i class="glyphicon glyphicon-shopping-cart" style="color:#FFD700"></i>
3737
Basket
38-
<span class="badge badge-pill badge-warning"><%= @current_cart.line_items.count %></span>
38+
<span class="badge badge-pill badge-warning" id="total_cart_items_badge"><%= @current_cart.line_items.count %></span>
3939
|
4040
<i class="glyphicon glyphicon-eye-open" style="color:firebrick"></i>
4141
Orders

app/views/layouts/application.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
</div>
2222
<%= render 'layouts/footer' %>
2323

24-
24+
<!-- <script type="text/javascript">
25+
$(function () {
26+
alert("hi");
27+
});
28+
</script> -->
2529

2630
</body>
2731
</html>

app/views/line_items/create.js.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$('#total_cart_items_badge').html("<%= @current_cart.line_items.count %>");

app/views/products/_product.html.erb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,27 @@
1919
<strong>
2020
Price :
2121
</strong>
22-
<%= product.price %> $
22+
<%= product.price %>
23+
$
2324

2425
<p>
2526
<%# if logged_in? %>
2627
<!-- Add to Basket -->
27-
<%=button_to line_items_path(:product_id => product.id) , class: "btn btn-warning" , style: "margin-top:15px;" do %>
28+
<%=button_to line_items_path(:product_id => product.id), :remote => true , class: "btn btn-warning" , style: "margin-top:15px;" , params:{productID: product.id} do %>
2829
<i class="glyphicon glyphicon-plus" style="color:black;"></i>
2930
Add to Basket
3031
<% end %>
31-
<%# end %>
32-
</p>
32+
<%# end %>
33+
</p>
3334

34-
<p>
35+
<p>
3536

36-
<!-- Only Admin is allowed to modify products -->
37-
<% if logged_in? && current_user.admin? %>
38-
<%= link_to 'Edit', edit_product_path(product) %>
39-
or
40-
<%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
41-
<% end %>
37+
<!-- Only Admin is allowed to modify products -->
38+
<% if logged_in? && current_user.admin? %>
39+
<%= link_to 'Edit', edit_product_path(product) %>
40+
or
41+
<%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
42+
<% end %>
4243

43-
</p>
44+
</p>
4445
</li>

config/initializers/wrap_parameters.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
77
ActiveSupport.on_load(:action_controller) do
8-
wrap_parameters format: [:json]
8+
wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
99
end
1010

1111
# To enable root element in JSON for ActiveRecord objects.

0 commit comments

Comments
 (0)