# Observers

**URL:** <https://www.ruby-forum.com/t/observers/83036>\
**Category:** Rails\
**Created:** [January 23, 2007, 6:44am UTC](https://www.ruby-forum.com/t/observers/83036 "2007-01-23T06:44:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swanand\_deodhar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@swanand\_deodhar](https://www.ruby-forum.com/u/swanand_deodhar)\
**Post date:** [January 23, 2007, 6:44am UTC](https://www.ruby-forum.com/t/observers/83036/1 "2007-01-23T06:44:56Z")

</div>

Hi!  
I want to do a functionality of wordcount. I thought of using  
observers  
for doing that. I attached an observer to the textarea and set the  
polling  
time as 1 second. Then in the controller i wrote the method called  
counter  
which will take the raw\_post and will count the words using length  
or something like that  
But my problem is in the observer it is not doing the updations.  
Here is my observer code:  
\<%= observe\_field (:value,  
:frequency=\>0.5,  
:update=\>‘count’,  
:url=\>{:action=\>:counter}) %\>  
What could be the problem. Plz help.  
Thnx and Regards,  
Swanand

---

<div class="post-metadata">

**Author:** ![swanand\_deodhar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@swanand\_deodhar](https://www.ruby-forum.com/u/swanand_deodhar)\
**Post date:** [January 23, 2007, 7:52am UTC](https://www.ruby-forum.com/t/observers/83036/2 "2007-01-23T07:52:03Z")

</div>

Do the logs show that the server is being hit? If not, make sure the  
right  
.js files are included.

swanand deodhar wrote:

> ```
> :frequency=>0.5,
> :update=>'count',
> :url=>{:action=>:counter}) %>
> 
> ```
> 
> What could be the problem. Plz help.  
> Thnx and Regards,  
> Swanand
> 
> >

–  
View this message in context:  
[http://www.nabble.com/-Rails--Observers-tf3062423.html#a8516360](http://www.nabble.com/-Rails--Observers-tf3062423.html#a8516360)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![swanand\_deodhar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@swanand\_deodhar](https://www.ruby-forum.com/u/swanand_deodhar)\
**Post date:** [January 23, 2007, 7:57am UTC](https://www.ruby-forum.com/t/observers/83036/3 "2007-01-23T07:57:06Z")

</div>

Hi

swanand deodhar wrote:

> Hi!  
> I want to do a functionality of wordcount. I thought of using  
> observers for doing that.

I think a better way for this specific wish would be to do it only in  
Javascript to don’t make any request when you don’t need.

After if you like to control the text\_area content on server side here  
is a way to do it.

> I attached an observer to the textarea and set  
> the polling time as 1 second. Then in the controller i wrote the method  
> called counter which will take the raw\_post and will count the words  
> using length  
> or something like that  
> But my problem is in the observer it is not doing the updations.  
> Here is my observer code:  
> \<%= observe\_field (:value,  
> :frequency=\>0.5,  
> :update=\>‘count’,  
> :url=\>{:action=\>:counter}) %\>

First things, be sure to have prototype library include.

In your layout you can have :

 ... \<%= javascript\_include\_tag :defaults %\> ...

once this is done, here is an working example of controller, view:

Controller:

class ItemsController \< ApplicationController  
def index  
@item = Item.new  
end

# …

def counter  
render :text =\> ‘

’+params[:name].length.to\_s+‘

’  
end  
end

View:

0

\<% form\_for :item, :url =\> {:action =\> :create} do |f| %\> \<%= f.text\_area :name %\> \<%= observe\_field (:item\_name, :frequency =\> 0.5, :update =\> 'count', :with =\> 'name', :url=\>{:action=\>:counter}) %\> \<% end %\>
## Hope this help 😉

Sébastien Grosjean - ZenCocoon  
seb.box.re [zencocoon.com](http://zencocoon.com)

---

<div class="post-metadata">

**Author:** ![swanand\_deodhar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@swanand\_deodhar](https://www.ruby-forum.com/u/swanand_deodhar)\
**Post date:** [January 23, 2007, 8:19am UTC](https://www.ruby-forum.com/t/observers/83036/4 "2007-01-23T08:19:09Z")

</div>

Hey guys!!  
Thnx a lot. Tht worked like a treat…  
Thnx and regards,  
Swanand
