Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

periodically_call_remote, simple_format

2006-04-26   [ 3 comments ]

Todays post will be relatively brief, however I expect that it will be somewhat helpful. Two methods - the first is periodically_call_remote, an ajax rich RoR feature; the second is simple_format an easy way to dump data out to the screen from a database with automatic < br /> and < p> tags.

Periodically_call_remote creates an ajax function that (as the name states) periodically calls a remote :url to acquire fresh data. An example of it on this site is the 'learn more' rotating text up above. Each time that text changes it is coming directly from my database with fresh random information. The code that makes the call for fresh data on my site is:

<%= periodically_call_remote :url => "http://www.forthecode.com/user/ajaxdump", :update => "myfunctions", :frequency => 8 %>


The options I used were:

:url - the url that the ajax request is made to
:update - the div tag to udpate with the new data
:frequency - the time in seconds between each call

Pretty simple, right? In my ajaxdump view (which is being called by the ajax above) I output data based on a few get variables that I also pass in the :url option as I see fit (often it is something like ?x=1 ).

Now on to the second function up for testing. This function is quite simple and is used in the view itself when outputing text (much like the h method covered in the last post).

<%= simple_format( @function.notes ) %>

This example is really simple - mainly because the function itself is easy to understand. In the above:

-simple_format takes the (text) and changes \n to < br /> and \n\n to < p>

So in my example it is simply replacing all the newlines in @function.notes with HTML break or paragraph tags.

This method is intended for use in views (rhtml files) but I am certain that they can be used in controllers if you really have a need for it. However, be forewarned that the function gave me this error when I tried to use it in my controller:

undefined method `content_tag'

I plan on looking into how to use the function in the controller (with or without purpose). At the moment I discover the way to do so I will leave a comment on this post with a 'how to:'. If someone beats me to it - all the more better!

:author => "Charles Abbott"
Converting to Ruby on Rails
 


navneet said:

Hi,
I'm running into the same issue. undefined method `content_tag' for #
Can you please let me know if you were able to figure out what the fix is?

Thanks
navneetaron at gmail dot com

2007-03-07 01:23:03 UTC

Charles said:

Hi navneet,

Unfortunately this relies on action_view which shouldnt be in your controller. However, the easiest way to work around this is create your own function with the same (or even better) code based on this function.

The source for this method (as copied from the Rails API).

def simple_format(text)
content_tag 'p', text.to_s.
gsub(/\r\n?/, "\n").
gsub(/\n\n+/, "</p>\n\n<p>").
gsub(/([^\n]\n)(?=[^\n])/, '\1<br/>')
end

As you can see it calls the method "content_tag" right off the bat - and this method is in another action_view helper. If you really want to create a similar function I suggest looking at the api (api.rubyonrails.org) to see what both these functions do.

2007-03-12 00:06:35 UTC

Charles said:

Oh, and after you make the method - place it in your application.rb to make it available to all your controllers (if you feel it should be).

Best of Luck!

2007-03-12 00:08:16 UTC

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"