gpt4 book ai didi

jquery - 没有路线匹配 [POST] - 将数据发送到 Ruby on Rails

转载 作者:行者123 更新时间:2023-12-01 01:19:04 26 4
gpt4 key购买 nike

我正在尝试通过 AJAX POST 请求将数据发送到我的 Ruby on Rails 应用程序。这在索引页面上工作得很好,它只接受 POST 请求并获取参数以供使用,例如 params[:MyParam],但是当尝试执行完全相同相同的操作时在我的 show 操作中,我收到错误:

No route matches [POST]

我像这样发送 POST 请求:

 <% if !params[:recData] %>
var PostDone = false;
<% end %>
$(document).ready(function() {
$.post(document.URL, { recData: "POST REQUEST!" }, function(response) {
if(PostDone == false) {
document.write(response);
PostDone = true;
}
});
});

我对 AJAX 和 Rails 不太熟悉,因此非常感谢您的帮助。

最佳答案

首先,如果您阅读 this你知道 resources :products 添加了以下路由

Verb    Path    action  used for
POST /products create create a new product

路线也是从顶部开始匹配的,所以既然你有

resources :products # matches '/products' via POST
match '/products' => 'products#index', :via => :post

每次您向 '/products' 发出 POST 请求时,都会匹配使用 resources :products 定义的第一个路由

show 操作之所以有效,是因为它也是使用 resources :products 定义的,但仅适用于 GET 方法

现在的解决方法

更改顺序为

match '/products' => 'products#index', :via => :post
resources :products # never matched '/products' via POST

或使用: except

resources :products, :except => :create # does not match '/products' via POST
match '/products' => 'products#index', :via => :post

在这两种情况下,您的 create 操作都会更新匹配

关于jquery - 没有路线匹配 [POST] - 将数据发送到 Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7024210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com