How to asynchronously load a partial page in rails

First put an empty, placeholder div in the main response

<div id="pink-dancing-elephants"></div>

and then add a little jQuery to the page

$.ajax({
    url: "/elephants/dancing",
    cache: false,
    success: function(html){
      $("#pink-dancing-elephants").append(html);
    }
});

and have the action that responses to /elephants/dancing/pink return the blob of HTML that you want to have fill up the div. In the action that is invoked by the AJAX request, you’ll want to render with :layout => false to keep the returned blob of HTML from including the entire frame. E.g.

# elephants_controller.rb
def dancing
  @elephants = #whatever
  render :layout => false
end

This would render the template in views/elephants/dancing.html.erb.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)