Forum: Ruby on Rails Observers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
833bc92fe8f46a9bfebae5afb65f7507?d=identicon&s=25 swanand deodhar (Guest)
on 2007-01-23 06:44
(Received via mailing list)
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
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2007-01-23 07:52
(Received via mailing list)
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....
Sent from the RubyOnRails Users mailing list archive at Nabble.com.
Cef1404ea693568ffa4d14e5683296b1?d=identicon&s=25 Sebastien Grosjean (zencocoon)
on 2007-01-23 07:57
(Received via mailing list)
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 ;-)
--
   S├ębastien Grosjean - ZenCocoon
   seb.box.re   zencocoon.com
833bc92fe8f46a9bfebae5afb65f7507?d=identicon&s=25 swanand deodhar (Guest)
on 2007-01-23 08:19
(Received via mailing list)
Hey guys!!
 Thnx a lot. Tht worked like a treat..
 Thnx and regards,
 Swanand
This topic is locked and can not be replied to.