Possible to generate html from a partial, then send html as response to ajax call?
I was wond开发者_开发技巧ering if I could re-use a partial that generates html.
I have a ajax request that passes text to my controller, and then it responds with some html.
The html it returns is the same html then I generate using a partial on the same view page.
Could I somehow re-use this partial in my controller action that responds to the ajax request?
You can check if it's AJAX request using request.xhr?
and specify :layout => false
to render just the partial file with no layout.
if request.xhr?
render :partial => 'partial_name', :layout => false
end
Maybe I understood the question differently, but you could do something like this to replace the div where you rendered the partial on the view page with an updated version, using the same template:
show.html.haml
.post
= render :partial => 'post
And in show.js.haml (the AJAX response)
== $('.post').html('#{escape_javascript(render :partial => 'post')}');
精彩评论