我可以使用路由路径进行js ajax调用吗?(Can I use a route path for a js ajax call?)

编程入门 行业动态 更新时间:2024-10-28 05:28:36
可以使用路由路径进行js ajax调用吗?(Can I use a route path for a js ajax call?)

我有一个在.js文件中运行的ajax调用,使用:

... update: function(){ $.ajax({ url: '/groups/order_links', ...

但我宁愿使用路线

我制作了文件扩展名.js.erb ,我尝试添加:

... update: function(){ $.ajax({ url: "#{order_links_groups_path}", ...

要么

... url: "#{order_links_groups_url}", ...

但我在任何一种情况下都得到404 - [HTTP/1.1 404 Not Found 76ms] 从POST http://localhost:3000/groups/49

rake routes显示我的路线包括:

... PUT /groups/:group_id/links/:id(.:format) links#update DELETE /groups/:group_id/links/:id(.:format) links#destroy order_links_groups POST /groups/order_links(.:format) groups#order_links groups GET /groups(.:format) groups#index POST /groups(.:format) groups#create new_group GET /groups/new(.:format) groups#new edit_group GET /groups/:id/edit(.:format) groups#edit

其定义如下:

resources :groups do resources :links collection do post 'order_links' end end

groups_controller有

class GroupsController < ApplicationController ... def order_links params[:link].each_with_index do |id, index| Link.where(id: id).update_all(['position = ?',index+1]) end render :nothing => true end ...

Rails 4.1

I have an ajax call that works in a .js file, using:

... update: function(){ $.ajax({ url: '/groups/order_links', ...

but I would rather use the route path

I made the file extension .js.erb and I tried adding:

... update: function(){ $.ajax({ url: "#{order_links_groups_path}", ...

or

... url: "#{order_links_groups_url}", ...

but I am getting a 404 in either case - [HTTP/1.1 404 Not Found 76ms] From a POST http://localhost:3000/groups/49

rake routes shows my routes include:

... PUT /groups/:group_id/links/:id(.:format) links#update DELETE /groups/:group_id/links/:id(.:format) links#destroy order_links_groups POST /groups/order_links(.:format) groups#order_links groups GET /groups(.:format) groups#index POST /groups(.:format) groups#create new_group GET /groups/new(.:format) groups#new edit_group GET /groups/:id/edit(.:format) groups#edit

which are defined with:

resources :groups do resources :links collection do post 'order_links' end end

groups_controller has

class GroupsController < ApplicationController ... def order_links params[:link].each_with_index do |id, index| Link.where(id: id).update_all(['position = ?',index+1]) end render :nothing => true end ...

Rails 4.1

最满意答案

"#{}"用于Coffeescript中的字符串插值,所以我假设这是一个错误。 我假设这个ajax请求所在的url是http://localhost:3000/groups/49因为如果你没有传入一个合适的url,那么它将使用当前路径。

"<%= order_links_groups_path %>"会在ruby中查找变量。 这可以工作,但资源目录中的JavaScript文件正在编译而不使用您的应用程序上下文。 含义order_links_groups_path将是未定义的。

这里的答案应该有所帮助: 在资产管道中路由助手

<% url = MyRailsApp::Application.routes.url_helpers %> url: "<%= url.order_links_groups_url %>"

"#{}" is used for string interpolation in Coffeescript so I am assuming that's an error. I assume the url where this ajax request is being made from is http://localhost:3000/groups/49 because if you don't pass in a proper url then it will use the current path.

"<%= order_links_groups_path %>" would look for a variable in ruby. This would work but JavaScript files in the assets directory are being compiled without using your apps context. Meaning order_links_groups_path will be undefined.

The answer here should help: Route helpers in asset pipeline

<% url = MyRailsApp::Application.routes.url_helpers %> url: "<%= url.order_links_groups_url %>"

更多推荐

本文发布于:2023-07-22 19:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   路由   路径   js   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!