0

I want to create and save data into cart model using jQuery/Ajax so, I want to call the controller's method in Ajax URL.

jQuery/Ajax script:

$(document).ready(function() {
  $('.add-cart-product').click(function() {
  var product_id = $('.simpleCart_shelfItem').data('product');
  var user_id = $('.simpleCart_shelfItem').data('user');
  $.ajax({
    type: 'GET',
    url: "/front/carts/add_cart",
    data: {
      product_id: product_id,
      user_id: user_id
    },
    success: function(data) {}
  });
 })
})

carts_controller.rb:

def add_cart
  @cart = Cart.new(:user_id => params[:user_id],:product_id => params[:product_id] )
  @cart.save
end

model/cart.rb:

class Cart < ActiveRecord::Base
  has_many :products
  has_many :users
end

routes.rb:

namespace :front do
  resources :carts do
    collection do
      get 'add_cart'
    end  
  end
end

These are all the routes which are generated from rake routes :

aspireedge@aspireedge-H81M-S:~/Projects/Ronak/store$ rake routes | grep carts
                 front_carts_show GET        /front/carts/show(.:format)                      front/carts#show
         batch_action_admin_carts POST       /admin/carts/batch_action(.:format)              admin/carts#batch_action
                      admin_carts GET        /admin/carts(.:format)                           admin/carts#index
                                  POST       /admin/carts(.:format)                           admin/carts#create
                   new_admin_cart GET        /admin/carts/new(.:format)                       admin/carts#new
                  edit_admin_cart GET        /admin/carts/:id/edit(.:format)                  admin/carts#edit
                       admin_cart GET        /admin/carts/:id(.:format)                       admin/carts#show
                                  PATCH      /admin/carts/:id(.:format)                       admin/carts#update
                                  PUT        /admin/carts/:id(.:format)                       admin/carts#update
                                  DELETE     /admin/carts/:id(.:format)                       admin/carts#destroy
                      front_carts GET        /front/carts(.:format)                           front/carts#index
                                  POST       /front/carts(.:format)                           front/carts#create
                   new_front_cart GET        /front/carts/new(.:format)                       front/carts#new
                  edit_front_cart GET        /front/carts/:id/edit(.:format)                  front/carts#edit
                       front_cart GET        /front/carts/:id(.:format)                       front/carts#show
                                  PATCH      /front/carts/:id(.:format)                       front/carts#update
                                  PUT        /front/carts/:id(.:format)                       front/carts#update
                                  DELETE     /front/carts/:id(.:format)                       front/carts#destroy
             add_cart_front_carts POST       /front/carts/add_cart(.:format)                  front/carts#add_cart
                                  GET        /front/carts(.:format)                           front/carts#index
                                  POST       /front/carts(.:format)                           front/carts#create
                                  GET        /front/carts/new(.:format)                       front/carts#new
                                  GET        /front/carts/:id/edit(.:format)                  front/carts#edit
                                  GET        /front/carts/:id(.:format)                       front/carts#show
                                  PATCH      /front/carts/:id(.:format)                       front/carts#update
                                  PUT        /front/carts/:id(.:format)                       front/carts#update
                                  DELETE     /front/carts/:id(.:format)                       front/carts#destroy
6
  • 2
    What is the problem? Do you get any error? Commented Jul 20, 2017 at 12:30
  • no not getting any error but URL not redirect to controller's method. Commented Jul 20, 2017 at 12:38
  • run rake routes and paste the result here Commented Jul 20, 2017 at 12:39
  • Is the JS even firing? Put a breakpoint and check whether that function being triggered or not. Commented Jul 20, 2017 at 12:41
  • @RonakBhatt check my post Commented Jul 20, 2017 at 12:47

2 Answers 2

1

try below code to solve issue of authenticity token:

class CartsController < ApplicationController
  protect_from_forgery except: "add_cart"

  def add_cart
   ...
  end
end
Sign up to request clarification or add additional context in comments.

Comments

0

You can actually have your ajax requests include the token, something like:

views/layout/application.html.erb

<meta name="authenticity-token" content="<%= form_authenticity_token %>" />

assets/javascript/application.js (assuming jQuery)

$(function(){
    $.ajaxSetup({
      headers: { 'X-CSRF-Token': $('meta[name="authenticity-token"]').attr('content') }
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.