19

I have an action that needs to render a view to string. The view is called index.xml.erb. I am trying to achieve this with render_to_string:

my_string = render_to_string(layout: false, format: "xml")

render_to_string is instead rendering the contents of index.html.erb and assigning it to my_string. What am I missing?

Note: I am aware that I can do something like this:

my_string = render_to_string(:action => "#{self.action_name}.xml.erb")

But I'm curious as to why the "format" option isn't honored with render_to_string.

1 Answer 1

28

This works for me.

render_to_string( :action => "#{self.action_name}", :formats => [:xml] )

Sign up to request clarification or add additional context in comments.

1 Comment

Geez, 7yrs later and this answer got me out of a headache trying to grab some pdf templates on a json request. Thanks for contributing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.