3

I have a ruby on rails website. The page is dynamically loaded and generated using ruby and rails. However, I'd like to also generate a static .html page to ease my server rather than calling the rails page every time.

In PHP I know how to capture the output buffer using ob_start() and ob_get_contents() to get the outputted text.

How do I capture the output from my rails page into a variable?

EDIT: The reason I want to do this is so that I can save my page as .html for use on other machines. So I generate the HTML using ruby and distribute to others in a format they can view.

3 Answers 3

9

You should use Rails caching to achieve this result. It achieves the ends you are looking for.

Alternatively, you can render_to_string and output the result using render:

 #ticket_controller.rb
 def TicketController < ApplicationController

   def show_ticket
     @ticket = Ticket.find(params[:id])

     res = render_to_string :action => :show_ticket
     #... cache result-- you may want to adjust this path based on your needs
     #This is similar to what Rails caching does
     #Finally, you should note that most Rails servers serve files from
     # the /public directory without ever invoking Rails proper

     File.open("#{RAILS_ROOT}/public/#{params[:action]}.html", 'w') {|f| f.write(res) }
     # or .. File.open("#{RAILS_ROOT}/public/#{params[:controller]}/#{params[:action]}/#{params[:id]}.html", 'w') {|f| f.write(res) }
     # or .. File.open("#{RAILS_ROOT}/snapshots/#{params[:controller]}/#{params[:action]}/#{params[:id]}.html", 'w') {|f| f.write(res) }
     render :text => res
   end
 end
Sign up to request clarification or add additional context in comments.

5 Comments

I think this is what I was looking for. Can you give more of a use case example of this render_to_string? Basically I'm looking to save the entire output of the page load to a .html file.
render_to_string should give you exactly what you are looking for, all of the HTML that Rails would have given to the browser, including layout. You can easily just save this to a file.
Where does this go though? In the controller? In the view?
This should go in your controller. Again, be very careful about caching in the public directory (as shown), but this is exactly what Rails does when caching for you (additionally, it prepends folders for current controller / action / id, ...). If you are just looking to 'snapshot', then I would suggest saving to "#{RAILS_ROOT}/snapshots/#{params[:controller]}/#{params[:action]}/#{params[:id}.html, or whatever would make sense for your situation
Your help and railsforum.com/viewtopic.php?id=41858 solved my problem. Thank you all.
2

You probably want to look into caching rather then saving the output of your rails app directly. Check out:

Comments

0

I ended up going with the following:

@page_data = render_to_string() # read the entire page's output to string

if (File.exist?('../cache.html'))
    file = File.open('../cache.html','rb')
    contents = file.read
else
    contents = ''
end
if (@page_data!=contents) # if the page has changed
    # save the output to an html version of the page
    File.open('../cache.html','w') {|f| f.write(@page_data) }
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.