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

Ajax search in Rails ...

Ajax is a must these days for websites. It is used in almost all applications and in many cases is used for the wrong reasons. One of those cases is however used for the right reason, Ajax search.

Ajax is a must these days for websites. It is used in almost all applications and in many cases is used for the wrong reasons. One of those cases is however used for the right reason, Ajax search.

In this tutorial I will be using the acts_as_ferret plugin to implement Ajax search into your view to return similar titles of posts. If you are unfamiliar with acts_as_ferret please read my acts_as_ferret Tutorial post to setup the plugin for you models.

Using the same model (Post) as my tutorial, I am going to implement AJAX search to search for similar post titles.
Javascript defaults

Ensure that in your layout.rhtml file you have a line to include the javascript defaults for Ruby on Rails.

1
2

   

# application.rhtml
  <%= javascript_include_tag :defaults %>

View

In your view you need to create a text field, and a observe_field Prototype helper.

1
2
3
4
5
6
7
8

   

# post/new.html.erb
...
<%= text_field(post, itle)  %>
 
<%= observe_field(post_title,  :frequency => 2,
    :update => isimilar_posts, :url =>
    { :controller => posts, :action=> ajax_search },
    :with => " isearch= + escape(value)") %>

Controller

In your controller add a method to handle the Ajax request. In this method use the find_with_ferret method to return a list of posts found by the search. Make sure you add render :layout => false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

   

# controllers/posts_controller.rb
...
  def ajax_search
    if params[ isearch]
      query =
      params[ isearch].split( ).each {|token|
        if(query != )
          query = query + OR + token.strip
        else
          query = query + token.strip
        end
      }
      @posts = Post.find_with_ferret(query, :limit => 5)
      render :layout => false
    end
  end

Ajax Partial

This is the HTML that will be generated by the ajax_search method in your controller.

1
2
3
4
5

   

# views/questions/ajax_search.html.erb
<strong>Similar Posts:</strong><br/>
<% for post in @posts%>
    <%= link_to(post.title, post_url(post)) %> - posted <%= time_ago_in_words(post.created_at)%> ago<br/>
<% end%>

Thats it! Not much to get Ajax searching for your web application! Please post a comment if you have any problems, or send me an email.

source: jumbabox

 View Full Story.
Posted at 11:35:23 am | Permalink | Posted in Javascript  

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 (111)
  • Articles (85)
  • Bookmarking (35)
  • Calendar (19)
  • Chat (39)
  • ColdFusion (3)
  • CSS (41)
  • Email (23)
  • Facebook (23)
  • Flash (15)
  • Games (6)
  • Google (28)
  • Html (14)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (36)
  • Javascript (171)
  • JSON (21)
  • Perl (2)
  • PHP (88)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (1)
  • Ruby (10)
  • Storage (4)
  • Toolkits (90)
  • Tutorials (199)
  • UI (12)
  • Utilities (167)
  • Web2.0 (13)
  • XmlHttpRequest (20)
  • YUI (4)

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