small medium large xlarge

09 Apr 2014, 15:52
Noel Peden (1 post)

At the end of chapter 1 you give the following example:

def another
    render pdf: "contents", template: "home/index"

And you can use that to call a template in another controller. This also works:

render "elsewhere/index"

And it also bypasses the renderer and goes straight to the template handler code.

But if you do the following:

def another
    render pdf: "elsewhere/index"

the template is sought in home/another or application/another.

I’ve found you can hack the prefix array inside the renderer:

if options[:template] == action_name && filename =~ /^([^\/]+)\/(.+)$/
    options[:prefixes] ||= []
    options[:prefixes].unshift $1
    options[:template] = $2

But this feels bad to me. Is there a proper way to handle this? Or should :template just be required if the format is passed to render? Does that require a resolver?

05 Jun 2014, 16:09
José Valim (62 posts)

The value you pass to :pdf is the filename of the generated pdf, not of the template to be rendered. That’s why giving no template option causes home/another to be looked up.

You can change this behaviour if you want, for example, you could make the value given to :pdf be the template name and use another option for the filename.

You must be logged in to comment