Search code examples
ruby-on-railsruby-on-rails-3erbactionviewactioncontroller

Specifying format: "xml" ignored with render_to_string


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.


Solution

  • This works for me.

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