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.

YWCA Hello
  • 2,997
  • 4
  • 29
  • 40

1 Answers1

28

This works for me.

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

hoyhoy
  • 6,281
  • 7
  • 38
  • 36
  • 3
    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. – RudyOnRails Feb 10 '19 at 01:53