Rails: Ajax: Changes Onload

Posted by Jay Godse on Stack Overflow See other posts from Stack Overflow or by Jay Godse
Published on 2010-01-19T18:55:39Z Indexed on 2010/03/21 1:01 UTC
Read the original article Hit count: 410

Filed under:
|
|

Hi.

I have a web layout of a for that looks something like this

<html>
  <head>
  </head>

  <body>
    <div id="posts">
      <div id="post1" class="post" >
         <!--stuff 1-->
      </div>
      <div id="post2" class="post" >
         <!--stuff 1-->
      </div>

      <!-- 96 more posts -->

      <div id="post99" class="post" >
         <!--stuff 1-->
      </div>
    </div>          

  </body>
</html>

I would like to be able to load and render the page with a blank , and then have a function called when the page is loaded which goes in and load up the posts and updates the page dynamically.

In Rails, I tried using "link_to_remote" to update with all of the elements. I also tweaked the posts controller to render the collection of posts if it was an ajax request (request.xhr?). It worked fine and very fast. However the update of the blank div is triggered by clicking the link. I would like the same action to happen when the page is loaded so that I don't have to put in a link.

Is there a Rails Ajax helper or RJS function or something in Rails that I could use to trigger the loading of the "posts" after the page has loaded and rendered (without the posts)?

(If putsch comes to shove, I will just copy the generated JS code from the link_to_remote call and have it called from the onload handler on the body).

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about AJAX