SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Victoria BC
    0 Post(s)
    0 Thread(s)

    Using AJAX to retrieve data

    I am looking for some general advice at this stage on the use of AJAX for searching. I have a requirement to do a search by name, address, city, postal code, etc. and the search should return a thumbnail sketch of the person's particulars (there could be up to 100 or more jones). Is Ajax the tool to do this kind of search or should I just use a standard form? If Ajax is the tool, can I capture the id of a selected person and use it in another form? With the possibility of more than 100 entries, can I page or scroll through the list? Any views on this issue would be appreciated.


  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    0 Post(s)
    0 Thread(s)
    You can use Ajax, or a normal form. I think it is a good idea to provide a GET-based search too, though. If you do, someone can post an url (on his blog, for example) to your search results:

    The common way to do Ajax in Rails is to generate HTML with Ruby, and then replace the contents of a div with that HTML. If you want to do this with Ajax, you could, for example have this set up:

    controller People:
    action search: displays the form which calls get_search_results
    action get_search_results: searches the database and returns the html.

    for example:

    def get_search_results
      @results =[:query])
    # get_search_results.rhtml
    <% for person in @results %>
    <li><%= %></li>
    The form in the search action loads this HTML into a div.

    If you want the user to be able to click on one of the results, and then load the data of this result into a form, you would use a link_to_remote in the <li><%= %></li>. This link calls an action update_form/12, where 12 is the id of the user. This action uses an rjs template like this (assuming you have a partial for the form):

    page.replace_html 'user_form', :partial => 'form', :object => @user
    This replaces the innerhtml of the div with id="user_form" with the output of the partial.

    I hope this is clear, but if you've never used Ajax on Rails, it will probably not be.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts