Hommer Smith
Hommer Smith

Reputation: 27852

render_to_string doesn't find my erb template?

I am trying to use render_to_string like this:

html_result = render_to_string(:template => 'template_160x600', layout: 'art_layout')

And I keep getting the following error:

Missing template /template_160x600

However, if I render normally, I don't have any problem:

render 'template_160x600', layout: 'art_layout'

What am I missing here?

EDITED:

I have added the view directory like this:

render 'arts/template_160x600', layout: 'art_layout'

But now I just get:

Missing template arts/show, application/show with

Upvotes: 2

Views: 1062

Answers (2)

Nobita
Nobita

Reputation: 23713

I believe that what is happening is that even though you are using render_to_string, Rails tries to do the default render for the show view afterwards.

So, if after render_to_string you don't want to render any view, just do this:

html_result = render_to_string(:template => 'art/template_160x600', layout: 'art_layout')
head :ok

or

html_result = render_to_string(:template => 'art/template_160x600', layout: 'art_layout')
render nothing: true

Upvotes: 1

infused
infused

Reputation: 24337

When you render a template you must specify the path from the root of the template search paths (app/views for one), so if your template is in app/views/art you would use:

html_result = render_to_string(:template => 'art/template_160x600', layout: 'art_layout')

Upvotes: 0

Related Questions