Observers

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

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

swanand deodhar wrote:

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 :

<head>
   ...
   <%= javascript_include_tag :defaults %>
   ...
</head>

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 => '<p id="count">'+params[:name].length.to_s+'</p>'
   end
end

View:

<p id="count">0</p>
<% 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 :wink:

Hey guys!!

Thnx a lot. Tht worked like a treat…

Thnx and regards,

Swanand