Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails html render to string

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!

like image 246
Bogdan Popa Avatar asked Dec 19 '22 12:12

Bogdan Popa


2 Answers

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)
like image 93
Bogdan Popa Avatar answered Feb 15 '23 15:02

Bogdan Popa


# 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"
like image 45
Sooraj Chandu Avatar answered Feb 15 '23 15:02

Sooraj Chandu