4

How can I render an HTML view into a string(saved in the database) to be able to render it back from it in rails?

def show_offer
  respond_to do |format|
    format.html { render 'offer_template_one', :layout => 'templates'}
    format.pdf do
      render :pdf => 'oferta',
      :template => 'templates/show_offer.pdf.erb',
      :layout => "layouts/templates.html.erb",
      :save_to_file => Rails.root.join('public', "Oferta.pdf")
    end
  end
end

This is the method through which I'm rendering my view

Thanks!

4
  • you want to cache everything or just this one page or just one part of this one page? Commented Aug 19, 2014 at 8:46
  • @dax sorry, I don't really want to cache it. I want to render it into a string. I've edited my question. Commented Aug 19, 2014 at 8:49
  • sorry but i am still not able to understand what is your requirement??? Commented Aug 19, 2014 at 8:59
  • @Milind I am rendering this offer_template_one.html.erb from my show_offer action and I want to also render it as a string and store it in the database. Commented Aug 19, 2014 at 9:04

2 Answers 2

9

Ok, I got it to work with render_to_string method:

@offer_string = render_to_string(:template => 'templates/offer_template_one.html.erb', :layout => false)
Sign up to request clarification or add additional context in comments.

1 Comment

You shouldn't even have to specify the template - it'll use the action name (with the extension of the format specified) by default and yield that to the layout of your choosing.
2
# Renders the clear text "hello world" with status code 200
render :text => "hello world!"

# Renders the clear text "Explosion!"  with status code 500
render :text => "Explosion!", :status => 500

# Renders the clear text "Hi there!" within the current active layout (if one exists)
render :text => "Hi there!", :layout => true

# Renders the clear text "Hi there!" within the layout
# placed in "app/views/layouts/special.r(html|xml)"
render :text => "Hi there!", :layout => "special"

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.