Skip to content

Instantly share code, notes, and snippets.

@johnkoht
Created March 6, 2014 04:41
Show Gist options
  • Save johnkoht/9382805 to your computer and use it in GitHub Desktop.
Save johnkoht/9382805 to your computer and use it in GitHub Desktop.
$('<%= j render partial: "cart_form", locals: {product: @product} %>').modal('show');
# This file already exists, you'll just append to it...
Spree::ProductsController.class_eval do
respond_to :js, :html
# custom method/endpoint that our ajax call will hit. This will respond to the ajax call and render
# a template in spree/products/add_to_cart.js.erb and you're @product varaible will be available
def add_to_cart
@product = Spree::Product.find(params[:id])
# No need for this.
# render partial: "spree/products/cart_form", locals: {product: @product}
end
end
# This adds a method to products called "add_to_cart" to the member (individual resource). If you run
# rake routes in your console you'll see it requires an id #=> /products/:id/add_to_cart
resources :products, only: [] do
get :add_to_cart, on: :member
end
-# Let's use a link tag and utilize Rails's Ujs AJAX mechanism. The remote: true will make this into an ajax
-# call to the server with the application/js data type. (see the controller below)
= link_to "BUY", add_to_cart_product_path(product.id), remote: true, class: "btn btn-secondary pull-left"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment