我有一个.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
"#{}"
用于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 %>"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句