# Like "observe\_field"

**URL:** <https://www.ruby-forum.com/t/like-observe-field/171666>\
**Category:** Rails\
**Created:** [July 7, 2009, 12:13pm UTC](https://www.ruby-forum.com/t/like-observe-field/171666 "2009-07-07T12:13:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 7, 2009, 12:13pm UTC](https://www.ruby-forum.com/t/like-observe-field/171666/1 "2009-07-07T12:13:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 8, 2009, 6:10am UTC](https://www.ruby-forum.com/t/like-observe-field/171666/2 "2009-07-08T06:10:55Z")

</div>

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](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…
