我有一个在.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 endgroups_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#editwhich are defined with:
resources :groups do resources :links collection do post 'order_links' end endgroups_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 %>"更多推荐
发布评论