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:

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.

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

On Wed, May 25, 2011 at 8:15 PM, Sebastian
[email protected]wrote:

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

On Wed, May 25, 2011 at 8:15 AM, Sebastian

On 25 May 2011 13:15, Sebastian [email protected] wrote:

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.

Have you considered using page caching instead?

Colin

On 26 May 2011 08:51, Sebastian [email protected] wrote:

Thank you all for your answers.

@Colin L.: Cache is only temporarily. I need to save the report
permanently, so cache is no option.

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!

Thank you all for your answers.

@Jim Ruther N.: 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 L.: 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

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 L.: 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

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.

Top-posted from Android

On Jun 1, 2011 9:39 PM, “Colin L.” [email protected] wrote:

On 1 June 2011 08:41, Sebastian [email protected] wrote:

SHORT UPDATE: > > Sorry! S…
It all seems a lot more complex than page caching.

Colin – You received this message because you are subscribed to the
Google
Groups "Ruby on Rails…

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!

On 1 June 2011 08:41, Sebastian [email protected] wrote:

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