Saturday, August 25, 2007

Simple AJAX concept : observe_field

Hi to all,

There are lot of ways to do AJAX in ruby on rails. This is one of the easiest way i found. Use the observe_field method.


observe_field(field_id, options = {})

Sample Code:

In the view u need to put this code:

Search By:

<%= select("listing", "search_by", [["City","1"],["Zip Code","2"],["State","3"]]) %>

<%=observe_field("listing_search_by", :url=>{:controller=>"search",:action=>"updatefields"},
:frequency => 0.5)%>

<label id="updatefields">Enter the City</label>

<%= text_field_tag :query%>

Note: frequency determines the speed ( in seconds ) if it less than zero its event time observer.

Controller code:

def updatefields
if @rvalue == "1"
@value="Enter the City"
elsif @rvalue == "2"
@value="Enter the Zip Code"
else @rvalue == "3"
@value="Enter the State"

The above sample code is used to display the label. Dynamically search text field's label is going to change... we can manipulate with the data avail in the database also(without reloading the window or browser. ie AJAX). This is easy to learn and simple to use and effective in the output.

Use and Enjoy.....

(If i made any mistake or u need any more clarification on this prompt me at