Justin Meltzer
Justin Meltzer

Reputation: 13558

Making an AJAX request go to the right URL

I have this ajax request inside some jquery code for autocomplete:

$.ajax({type:"POST", url:("showable_videos/create.js"), data:{video:{profile:val}}});

The autocomplete acts on a field that is inside my video show view. Therefore, when it makes the AJAX request I get this error:

Started POST "/videos/showable_videos/create.js" for 127.0.0.1 at Tue Apr 26 00:18:33 -0700 2011

ActionController::RoutingError (No route matches "/videos/showable_videos/create.js"):

Rails is prepending /videos/ to the URL when I want it to just be /showable_videos/create.js. How can I fix this?

Upvotes: 1

Views: 1364

Answers (3)

Ben Scheirman
Ben Scheirman

Reputation: 40991

since you didn't provide an absolute path (one rooted with "/") rails interprets that as "from the current path" instead of "from the root".

It's best to utilize rails path helpers instead of hard coding the urls like that.

Upvotes: 1

hammar
hammar

Reputation: 139900

Make it an absolute URL

 ... url:("/showable_videos/create.js") ...

Upvotes: 2

Austin Taylor
Austin Taylor

Reputation: 5477

Put a leading slash on your url: /howable_videos/create.js. Without that, the browser treats it as relative to the current directory.

Upvotes: 2

Related Questions