简体   繁体   English

Rails ajax:无法加载资源:服务器以状态404(未找到)响应

[英]Rails ajax: Failed to load resource: the server responded with a status of 404 (Not Found)

I'm trying to rewrite some actions in my app with ajax. 我正在尝试使用Ajax重写应用程序中的一些操作。

items_controller: items_controller:

...
def add_to_cart
  @cart = Cart.where(id: session[:cart_id]).first
  @cart = Cart.create if @cart.nil?
  session[:cart_id] = @cart.id
  session[:cart_items] = @cart.items
  session[:cart_items] << @item
  redirect_to root_url
end
...

items.js: items.js:

jQuery(function($) {
  $(".addtoCart").click( function() {
    var current_item = $(this).parents('tr')[0];
    if(confirm("Add to cart")) {
      $.ajax({
        url: '/items/' + $(current_item).attr('data-item-id') + '/add_to_cart',
        type: 'POST'
      });
    };
  });
});

view file: 查看文件:

%tr{"data-item-id" => "#{i.id}"}
  %td
    %span.addtoCart Add to cart

routes: 路线:

Store::Application.routes.draw do

  root 'items#index'

  resources :items

  resources :items do
    get :add_to_cart, on: :member
  end

end

When I click on Add to cart there is an error Failed to load resource: the server responded with a status of 404 (Not Found) and POST http://localhost:3000/items/13/add_to_cart 404 (Not Found) although /items/13 is exists. 当我点击Add to cart存在错误Failed to load resource: the server responded with a status of 404 (Not Found)POST http://localhost:3000/items/13/add_to_cart 404 (Not Found)尽管/items/13存在。 Where did I made a mistake? 我在哪里弄错了?

error stacktrace: 错误堆栈跟踪:

Started POST "/items/13/add_to_cart" for 127.0.0.1 at 2014-03-30 02:55:03 +0400

ActionController::RoutingError (No route matches [POST] "/items/13/add_to_cart")

Thanks! 谢谢!

Change your routes as below: 如下更改路线:

  root 'items#index'

  resources :items do
    post :add_to_cart, on: :member
  end

I converted the add_to_cart request to a post rather than get because you are firing a post request and have defined the route as get . 我将add_to_cart请求转换为post而不是get因为您正在触发post请求并将路由定义为get Also, I removed the duplicate route defined resources :items and 另外,我删除了重复的路径定义resources :items

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载资源失败:服务器响应状态为404(未找到)。 与在caph ajax - Failed to load resource: the server responded with a status of 404 (Not Found). with ajax in caph JS / AJAX无法加载服务器响应的资源,状态为404(未找到) - JS/AJAX failed to load resource the server responded with a status of 404 (not found) 程序无法加载无法加载资源:服务器以404(未找到)状态响应 - Program will not load Failed to load resource: the server responded with a status of 404 (Not Found) Node.js服务器:无法加载资源:服务器以状态404(未找到)响应 - Nodejs server: Failed to load resource: the server responded with a status of 404 (Not Found) 加载资源失败:服务器响应状态为 404(未找到)? - Failed to load resource: the server responded with a status of 404 (Not Found)? 加载资源失败:服务器响应状态为404(未找到) - getting Failed to load resource: the server responded with a status of 404 (Not Found) 加载资源失败:服务器以 404(未找到)状态响应 nodejs - Failed to load resource: the server responded with a status of 404 (Not Found) with nodejs Apache Cordova:“无法加载资源:服务器以404(未找到)状态响应” - Apache Cordova: “Failed to load resource: the server responded with a status of 404 (Not Found)” ReactJS - 加载资源失败:服务器响应状态为 404(未找到) - ReactJS - Failed to load resource: the server responded with a status of 404 (Not Found) 错误:加载资源失败:服务器响应状态为 404(未找到) - Error: Failed to load resource: the server responded with a status of 404 (Not Found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM