views:

174

answers:

2

Hi,

I want a rails method to be called automatically whenever the contents of a textfield is changed, or every n seconds. How can I do this? The method is on the object's model definition. Also, how can I get the content of the textfield?

Thanks.

A: 

This will require using some javascript. I use the jQuery library to help me along. I would search the net ('Google') "rails jquery" for some tutorials.

Or if you prefer another library check out http://stackoverflow.com/questions/951141/best-javascript-libraries-to-work-with-rails

Once you have your javsacript library in place you will need some sort of script to monitor the input. One way is to use a keyup event to listen for activity. This script will then post your data to the server via ajax.

The Who
I already know how to check if a certain element changed. Thats pretty easy using railsobserve_field("content", :frequency => 1, :function => "alert('Element changed')")I just don't know how to call the object's method.
André
Instead of using `observe_field("content", :frequency => 1, :function => "alert('Element changed')")` which will run some javascript use `observe_field("content", :frequency => 1, :url => "controller/method/path" :with => "'text=' + value" )`. This will pass the value to a method in your controller. You can then do your call to your model and return whatever to your view.
JosephL
For monitoring the field ever n seconds you might want to checkout the settimeout js function .
NM
A: 

You may also use built-in rails helpers and write something like

<%= periodically_call_remote :url => {:controller=>'mailbox',
                                        :action => 'get_newest_messages'},
        :frequency => 15
%>

And then in the specified controller you may call needed method of a model.

Sergii Vozniuk