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!

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
2007-03-07 01:23:03 UTCnavneetaron at gmail dot com
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 UTCCharles 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