I have a RoR app that tracks the status of values in a database and displays the visualization of this information in the form of charts, graphs, and tables generated by an erb file. This is very handy and I am able to save snapshots of the status' of the DBs by simply saving the page when I open it in a browser. What I would like, however, is for my app to automatically do this saving for me on a nightly basis. I assume this is possible but I'm not having much luck with this so far. Any suggestion on this point would be very helpful.
-
are these charts and graphs generated dynamically from the single HTML? if they are separate files then solution will be more complicated because you will have to save more filesgertas– gertas2011-03-30 20:04:42 +00:00Commented Mar 30, 2011 at 20:04
-
Yes they are, I have graphs that are built using google's chart tools. I simply have the url's in the erb file and I fill out the important bits with information pulled from the DB.keybored– keybored2011-03-31 16:15:22 +00:00Commented Mar 31, 2011 at 16:15
2 Answers
you can always use render_to_string method to render your erb. For example you need to render show.html.erb for show action in statistics controller:
my_page = render_to_string :controller => 'statistics', :action => 'show', :layout => 'application'
but firstly you should define all it's variables which you use int show view.
@data = Data.last_data
@users = User.active
enter code here
my_page = render_to_string :controller => 'statistics', :action => 'show', :layout => 'application'
snapshot = Snapshot.new :page => my_page
snapshot.save
API: http://apidock.com/rails/ActionController/Base/render_to_string
1 Comment
You can use whenever: https://github.com/javan/whenever
From the readme:
Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.
There is also delayed-job: https://github.com/tobi/delayed_job
From the Readme:
Delayed_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background.