O'Reilly logo

Crafting Rails 4 Applications, 2nd Edition by José Valim

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Taking It to the Next Level

Going back to our renderer implementation, we now understand what happens when we add the following line to our controllers:

 
format.pdf { render pdf: ​"contents"​ }

In our renderer, it becomes this:

 
pdf = Prawn::Document.new
 
pdf.text render_to_string({})
 
send_data(pdf.render, filename: ​"contents.pdf"​,
 
disposition: ​"attachment"​)

When we invoke render_to_string with an empty hash, the _normalize_options method in the rendering stack detects the empty hash and changes it to render the template with the same name as the current action. At the end, render_to_string({}) simply passes template: "#{controller_name}/#{action_name}" to the view-renderer object.

The fact that our renderer relies on render_to_string ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required