How to render XML template and then use SEND_DATA in Ruby on Rails 3.2.8?

Dmitry picture Dmitry · Sep 26, 2012 · Viewed 7.4k times · Source

Can anybodyes help me with XML template rendering and send_data?

I have a controller:

def show
  @calculation = Calculation.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @calculation }
    format.xml {send_data( :partial=>show.xml.erb, :filename => "my_file.xml" ) }
    format.pdf { render :format=>false}
  end
end

But I have many errors with "stack level too deep"

If I use

{send_data( @calculation, :filename => "my_file.xml" ) }

I get XML file, but not from my template...

EDIT: I've got a way!

format.xml do  
  stream = render_to_string(:template=>"calculations/show" )  
  send_data(stream, :type=>"text/xml",:filename => "test.xml")
end

And all works properly!

Answer

DreadPirateShawn picture DreadPirateShawn · Oct 9, 2013

Copying the answer from the edited question body in order to remove this question from the "Unanswered" filter:

format.xml do  
  stream = render_to_string(:template=>"calculations/show" )  
  send_data(stream, :type=>"text/xml",:filename => "test.xml")
end

~ answer per Dmitry