Store a whole page as html in database

Hi,

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.

Sebastian

Similar question:

Is it possible to store the output of a rendered partial as html?

Sebastian

As long as it isn't too large for a text column in your DB, sure.

Walter

Hi,

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:

This is some text

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.

Just a quick reply. If you’re going with the save to db approach, I think you might

want to look at the render_to_string method.

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.

C

Have you considered using page caching instead?

Colin

Thank you all for your answers.

@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?

Kind regards, Sebastian

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.

Colin

render_to_string worked perfectly!

I just have to this in the controller and not in the view.

Controller:

@reportstring = render_to_string(:partial => "shared/ compare", :collection => @changedfamilies)

Then I can save the @reportstring in my database and call the whole view wherever I need it!

Thank you!

Sebastian wrote in post #1001166:

render_to_string worked perfectly!

I just have to this in the controller and not in the view.

Controller:

@reportstring = render_to_string(:partial => "shared/ compare", :collection => @changedfamilies)

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.

OK...I still have a problem:

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.

Sebastian

SHORT UPDATE:

Sorry! Stupid idea! I can of course just move my method into the same controller, so I don't have to leave it.

I have the following in my controller:

  def checkall     ->check all entries for updates<-

    create_report -> This should just run my method below asynchronously   end

  def create_report     ->this creates my report<-   end   handle_asynchronously :create_report, :priority => 3

It all seems a lot more complex than page caching.

Colin

Top-posted from Android

I am completely stuck!!!

This one is working in my controller, BUT NOT as a delayed_job:

  def checkall     #check all entries for updates as delayed_job

    create_report #should run my method below asynchronously   end

  def create_report     #this creates my report, but not working delayed!   end   handle_asynchronously :create_report, :priority => 3

So maybe the caching approach is really a better solution :wink:

How can I cache the page I need as a delayed_job?

I need to complete the 'checkall' method before creating my report!

Sebastian

Recently I got my first approach working!

I only had to edit the following (this_here)to call my method out of my controller:

  def checkall     #check all entries for updates as delayed_job

    this_here = MyController.new     this_here.create_report #run my method below asynchronously   end

  def create_report     #this creates my report, but not working delayed!   end   handle_asynchronously :create_report, :priority => 3

I don't know if this is good coding, but it is working!