17

I am having difficulty with the rails render_to_string function. I have created an app using the --api flag, so i think this may be the issue as i have tested in 'full' rails apps and its works just fine.

Essentially i am calling:

body_html = render_to_string(template: 'reservations/print')

I have also tried

body_html = render_to_string('reservations/print')
body_html = render_to_string(partial: 'reservations/print')
body_html = render_to_string(partial: 'reservations/print.html.erb')

which should return the html for that template. The filename is print.html.erb and just has basic data i.e.

<p> Hello world. </p>

When i output the body_html it is empty.

I referenced this SO question What is the correct way to render_to_string in wicked pdf? and am also going to use Wicked PDF to generate a pdf file.

Many thanks

Community
  • 1
  • 1
Dudedolf
  • 505
  • 1
  • 6
  • 18
  • Referencing, the stack overflow question. Did you try passing just `reservations/print` without the template keyword? – Dan Rubio Nov 08 '16 at 16:08
  • I don't think the `--api` flag is the problem. According to the documentaiton on Rails Api, `AbstractController::Rendering` comes in default and this Module contains the `render_to_string` method. – Dan Rubio Nov 08 '16 at 16:15
  • Hi Dan, thank you for taking the time to answer. I have that and a few more. I will update the question. – Dudedolf Nov 08 '16 at 19:29
  • sorry to add another comment, just wanted some fresh eyes if possible – Dudedolf Nov 09 '16 at 21:40

4 Answers4

18

Whilst I do not think this is the best solution doing the following worked.

ac = ActionController::Base.new  

html_string = ac.render_to_string(template: 'path_to_file') # e.g. 'posts/show'

The Application Controller file in Rails API mode inherits from ActionController::API, so I guess the functionality to render is in ActionController::Base.

Capripot
  • 1,354
  • 16
  • 26
Dudedolf
  • 505
  • 1
  • 6
  • 18
  • 1
    This should no longer be required (but I haven't tested). See module ActionController::Renderers::ClassMethods: "Both ActionController::Base and ActionController::API include ActionController::Renderers::All, making all renderers available in the controller." – webaholik Aug 11 '19 at 00:31
4

Try the following snippet code:

ActionController::Base.new.render_to_string("mailer/your-html-file", locals: { data: your-json-data-that-you-wanna-add-to-your-html-file })
Hossein Safari
  • 252
  • 3
  • 15
2

You can do something like this inside your action:

     respond_to do |format|
       format.html { render layout: 'your_layout' } # this is for your html responses if you need to respond with html
       format.json { # this block will execute for your json requests

        html = render_to_string action: :index, layout: false, :formats => [:html]
        render json: {data: html}
     }
Miguel Salas
  • 692
  • 1
  • 6
  • 21
0

This is the solution that work fine for me:

render_to_string partial: 'participant_data.html.erb'

The file must be named like this:

tickets/_participant_data.html.erb
Iwan B.
  • 3,982
  • 2
  • 27
  • 18