I want to create some reports on my app that are stored. So it is not
a big deal to create a view where my report is shown out of some
existing activerecords.
But how can I save what I see? Meaning save the whole page as report
in the database. I thought the easiest way is to store the whole page
in a long string with html markup. So I can easily render that value
later with 'raw' method.
An example:
store this as activerecord:
<p>This is some text</p>
call that in a later view for showing the report:
<%= raw Report.find(12).text %>
Is that the best approach or is there another one? If it is the best
approach, is there a way to get the html code of the recent page to
store that in an activerecord.
That would work. You may want to consider if it is just HTML text, or if the reports reference any images, video, additional files…, and whether you want to keep them too.
If you have a complex HTML page, and you want to use the DB approach (instead of just saving the file directly in disk and keeping the path in the DB), you may want to take a look to MHTML, parse the HTML and encode all the page elements using that format.
@Jim Ruther Nill: render_to_string sounds exactly like what I need,
but I get an error of 'undefined method'. I am using this code in
view:
<%= render_to_string(:partial => "shared/compare", :collection =>
@changedfamilies) %>
@Colin Law: Cache is only temporarily. I need to save the report
permanently, so cache is no option.
@C.A.C.D: I only have html with colored text and tables on it. So your
approach with MHTML is only for complex html's, right?
A page cache can be as permanent as you like. It is up to you when
you expire it. If you never expire it then it will be permanent. By
doing it yourself you are just re-inventing the wheel. The work has
already been done for you by Rails developers.
Then I can save the @reportstring in my database and call the whole
view wherever I need it!
Do you need this solution to scale? Fetching that large string from the
database is going to be far less efficient than Colin's suggestion about
using page caching. The web server was designed to be highly efficient
at serving static HTML content (i.e the cached pages). Using Rails and
the database can't possibly match the performance of Apache or Nginx.
The render_to_string method is only available in controllers and not
in models and views!!!
My approach was to check with several delayed_job my database entries
for changes (compare with external webservice!) and then run a
delayed_job for create the report with lower priority afterwards.
That is not working because I can't use render_to_string in a model
for the delayed_job.
Has anyone an idea how to solve this?
Or is it possible to delay a method from another controller?
I am so close! If I just call the method on a view the whole page is
stored as I want. I only need to do this in a delayed_job...!
@Colin Law: I had a look into caching, but I didn't understand the
whole thing, so I think for me it would be easier if my first approach
would work.