So I have a situation with an ajax call that is currently functioning in a .js file, utilizing:
...
update: function(){
$.ajax({
url: '/groups/order_links',
...
However, my preference would be to use the route path instead.
To achieve this, I changed the file extension to .js.erb and attempted the following:
...
update: function(){
$.ajax({
url: "#{order_links_groups_path}",
...
I also tried:
...
url: "#{order_links_groups_url}",
...
Unfortunately, I am encountering a 404 error in both cases - [HTTP/1.1 404 Not Found 76ms]
This error originates from a POST request to http://localhost:3000/groups/49
Upon executing rake routes
, it becomes evident that 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
These routes are defined as follows:
resources :groups do
resources :links
collection do
post 'order_links'
end
end
In the GroupsController
you'll find:
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
...
Environment: Rails 4.1