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 ...

Get Crafting Rails 4 Applications, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.