• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

An Introduction to using AJAX with Rails ...

The Rails framework comes bundled with the Prototype and Scriptaculous Javascript libraries. In this entry I'll introduce how to use those libraries in a Rails application.

Test the BlogDemo Project

   1. Open the BlogDemo project.
   2. Note, if you are starting with the provided BlogDemo project, you will also need to create the BlogDemo_development database and run the migrations.
   3. Run the project and browse to http://localhost:3000/blog to verify that it works.

The Plan

Currently, when a new comment is added, the entire page - blog entry and comments - are reloaded. Instead, we will use AJAX to only reload the comments. Just for fun, we will also show how to apply a visual effect to highlight the most recently added comment.
Preparation

First we are going to move the fragment of rhtml that we want to dynamically reload into a partial template.

   1. Ctrl+N to create a new RHTML file. Name the file _comments and place it in the appviewslog folder.
   2. Alt+Shift+O to open show.rhtml.
   3. Cut the following block of code from show.rhtml:



      And paste it into _comments.rhtml, replacing all existing content in _comments.rhtml.

   4. Return to show.rhtml and insert the following call to the partial from where you cut the code. Note, the <div> will be used later to reference this chuck of code when we dynamically reload the comments:

      <div id="comments">
          <%= render(:partial => "comments", :object => @post_comments) %>
      </div>

   5. Test your changes. The application should behave as it did before.

Change the POST to an XMLHTTPRequest

   1. First, let is make sure the JavaScript libraries we are going to use are included in our application. Alt+Shift+O to open blog.rhtml and add the following line below the stylesheet_link_tag:

       <%= javascript_include_tag :defaults  %>

      We are passing :defaults as the source because we are going to be using the Prototype and Scriptaculous libraries that come bundled with Rails.

   2. In show.rhtml, change the form_tag, which performs a HTTP POST, to a form_remote_tag, which performs an XMLHTTPRequest, as follows:

      <% form_remote_tag :url => {:action => "post_comment"} do %>


   3. Test. The entire show page is still reloaded on submit. This is because the post_comment action in blog_controller forces the reload by calling the show action.



   4. Comment out the redirect_to call in the post_comment action and try again. Things are getting worse, as now we get a Template is missing error:
source: weblogs
visit part 2

 View Full Story.
Posted at 08:31:37 am | Permalink | Posted in Java  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Be the first ... |Add your comment.

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


Top Stuff

e-messenger

MessengerFX

eBuddy

ILoveIM

AIM Express

Top 20 Ruby CMS


Our Partners

Facebook Applications

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines

Stock Exchange Chat


About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (114)
  • Ajax (10)
  • Ajax Games (9)
  • Articles (94)
  • Bookmarking (35)
  • Calendar (19)
  • Chat (40)
  • ColdFusion (3)
  • CSS (48)
  • Email (23)
  • Facebook (41)
  • Flash (17)
  • Google (30)
  • Html (16)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (40)
  • Javascript (190)
  • jQuery (3)
  • JSON (24)
  • Perl (2)
  • PHP (97)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (1)
  • Ruby (15)
  • Storage (4)
  • Toolkits (94)
  • Tutorials (203)
  • UI (12)
  • Utilities (173)
  • Web2.0 (18)
  • XmlHttpRequest (22)
  • YUI (4)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange