Rockwell Rice Rockwell Rice - 1 year ago 82
Ajax Question

No route matches [GET] error when sending a post request with ajax

Sending a post request via ajax but getting the error above, "No route matches [GET]".

ajax looks like this

type: 'POST',
url: '/stage_progress',
data: {'subject_id' : subject, 'course_id' : course, "chapter_id" : chapter, "stage_id" : stage, "user_id" : user, "session" : "0", "completed" : "true"},
success: function(){
console.log('got it');


post '/stage_progress', to: 'stage_progress#track_progress'

If I run rake routes it shows that a post route to this url exists but for some reason the ajax is creating a "get" request. Any help explaining why and how to fix it would be greatly appreciated.

Answer Source

When looking into your code, you are overriding the default index action of your controller 'stage_progress'. Calling '/stage_progress' will only route to the default index action(get). I guess you either rename the route '/stage_progess' to something else and that would solve the issue.

Or in other case you still want the same route, then try writing it above all other routes,

post '/stage_progress', to: 'stage_progress#track_progress'
resources 'stage_progress'

Because the routes are always mapped from top to bottom in the routes.rb file. If there is any other 'GET' route above the 'POST' route declaration, it will still take only the first route configured.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download