如何在rails中的jQuery / ajax url中调用控制器的方法?

时间:2017-07-20 12:23:29

标签: javascript jquery ruby-on-rails ruby ajax

我想使用jQuery / Ajax创建数据并将其保存到购物车模型中,因此,我想在Ajax URL中调用控制器的方法。

jQuery / Ajax脚本:

$(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

这些是从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

2 个答案:

答案 0 :(得分:0)

尝试以下代码来解决authenticity token

的问题
class CartsController < ApplicationController
  protect_from_forgery except: "add_cart"

  def add_cart
   ...
  end
end

答案 1 :(得分:0)

您实际上可以让您的ajax请求包含令牌,例如:

视图/布局/ application.html.erb

system()

assets / javascript / application.js(假设是jQuery)

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