views:

47

answers:

1

I am trying to create some kind of search in my ruby on rails application and I want to add ajax support. I want when a user types in a search box a word, ajax automatically generates the results. And I have searched almost all internet and I couldn't find answer to following question:

When I type into my text area nothing happens. It is like ajax is not working at all. Tre problem is in observe_field which should observe my id= 'query' but it seem like it not doing anything

I am using Prototype 1.6.1 and the newest version of rails. The code I wrote is:

viewer:

    <form name="sform" action="" style="display:inline;">
<label for="item_name">Filter on Property No  : </label>
<%= text_field_tag(:query, params['query'], :size => 10, :id => 'query' ) %>
</form>

<%= image_tag("/images/system/blue_small.gif",
              :align => "absmiddle",
              :border => 0,
              :id => "spinner",
              :style =>"display: none;" ) %>
</p>

<%= observe_field 'query',  :frequency => 1,
         :update => 'table',
         :before => "$('#spinner').show()",
         :success => "$('#spinner').hide()",
         :url => {:action => 'list'},
         :with => 'query' %>

<div id="table">
<%= render :partial => "items_list" %>
</div>

And my controler is:

  def list


    sort = case params['sort']
           when "Title"  then "title"
           when "address"   then "address"
           when "close date" then "close_date"
           when "property_no_reverse"  then "property_no DESC"
           when "address_reverse"   then "address DESC"
           when "close_date_reverse" then "close_date DESC"
           end

    conditions = ["title LIKE ?", "%#{params[:query]}%"] unless params[:query].nil?

    @total = Message.count(:conditions => conditions)
     @accounts = Message.paginate :page => params[:page], :per_page => 10, :order => sort, :conditions => conditions
    if request.xml_http_request?
      render :partial => "items_list", :layout => false
    end

  end

I would greatly appreciate any help I can get!

A: 

You said your using the newest version of Rails, if that is Rails 3.0.0 then observe_field and the way they are doing javascript is definitely different. Either downgrade to Rails 2.3.8 (I would not), learn the new way, or use the old way in Rails 3.0.0

New Way in Rails 3.0.0: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/

Old way in Rails 3.0.0: http://github.com/rails/prototype_legacy_helper

If you want 2.3.8 I can show you observe field, just let me know...

Nick