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 i开发者_开发知识库t to just be /showable_videos/create.js
. How can I fix this?
Put a leading slash on your url: /howable_videos/create.js
. Without that, the browser treats it as relative to the current directory.
Make it an absolute URL
... url:("/showable_videos/create.js") ...
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.
精彩评论