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!
