I am currently in rails 3.1
. I wanna achieve ajax call in jquery with out the use of form_for method.
My current page is localhost:3000/posts/show/2
and I have a button in the page and it does not belong under any form. On click of that button, I have to send an ajax call to the create
action in the Comments
controller and this is the jquery code
$(function(){
$('#add_button').click(function(){
$.post('comments/create', function(data) {
$('.res开发者_运维百科ult').html(data);
});
return false;
});
});
but this didnt end in an ajax call but it was a browser call and got an error saying could not find the method in Posts
controller. I even tried changing the url in $.post
to Comments.create
(fyi, I have added necessary changes in routes.rb) but still i end up getting a browser call and an error.
How can i restrict it to pure ajax call and how to access the correct url(i.e. to access action of a comments controller inside the view of a Posts controller)?
If you want to do it 'the rails way' I would suggest you read this excellent post: http://www.alfajango.com/blog/rails-3-remote-links-and-forms/
That way you'll be able to bind your code to rail's remote link code.
精彩评论