Like "observe_field"

I don’t see where this can be done in rails directly, but I am hoping
some creative mind can see how to use dvarious rails pieces to do it.

I need an ajax request like “observe_field” that triggers an action so
as to update something, but rather than a field in a form, I want to
observe a file containing json data on a server, such that when the
data changes, I read it, and perform an action with it to update
something.

Does anyone have any idea how I might accomplish this in RoR? Thanks,
Janna B

Janna Brossard wrote:

I don’t see where this can be done in rails directly, but I am hoping
some creative mind can see how to use dvarious rails pieces to do it.

I need an ajax request like “observe_field” that triggers an action so
as to update something, but rather than a field in a form, I want to
observe a file containing json data on a server, such that when the
data changes, I read it, and perform an action with it to update
something.

Does anyone have any idea how I might accomplish this in RoR? Thanks,
Janna B

Hi Janna,

I could be mistaken but I don’t think you can use observe_field in that
way. It has to observe a DOM ID and the closest I think you could use
in the DOM Location Object, but am unsure…

I know how you can read JSON data from the file though:

require ‘open-uri’
require ‘json’

url = ‘http://search.twitter.com/trends.json

buffer = open(url, “UserAgent” => “Ruby-Wget”).read

convert JSON data into a hash

result = JSON.parse(buffer)

trends = result[‘trends’]
trends.each do |subject|
puts subject[‘name’] + ’ ’ + subject[‘url’]
end

If I were having to automate this I would create a rake task that called
a ruby file such as the one listed here that would read the json data
from the file, and then update the database with the new information…

You could create a cron task to automate the rake task every 30 minutes
or 1-hour depending…