How do I make an ajax call and then update a div with a partial? I've
tried this but it's not working. Comment is created but the partial is
not loaded.
In rails 3, handleling an Ajax request is too easy with Ujs. I’ll try to give you a good example.
I suppose, i want to ‘activate’ post an article via an Ajax call :
I have :
posts_controller
Route match for activating in Routes.rb file
Views/posts/ directory where views related t o ‘posts’ are located.
And of course a model (optional for our case)
Suppose that i have a confirm link that’s alow me to ‘activate’ a particular ‘post’.
<%= link_to ‘activate’ , {:controller => “posts”, :action => “activate”, :id => post.id}, :remote => true %>
I also have in Routes.rb file this satement :
match “posts/activate/:id” => “posts#activate”
I have in the Posts_controller file the action ‘activate’ :
def activate
respond_to do |format|
format.js { }
end
end
And finally, you have to create a file named ‘activate.js.erb’ and put this test line of code for ajax responding :
So how do I make a basic ajax call from a link, do something in an
action and update a div with new content?
If you want to follow the jqueryish way, then you setup a click
handler on the link as your first post does, but your success function
should be more along the lines of
function(data){
$('#commentlist').html(data)
}
(which assumes that your action is producing a chunk of html in
response)
Try using Firebug in Firefox to check there are no script errors.
Then copy the complete html source from the page (View > Page Source
or similar in browser) and paste it into the w3c html validator to
check the html is ok.