Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 494
Dmitry Avatar asked Sep 26 '12 12:09

Dmitry


1 Answers

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

like image 63
DreadPirateShawn Avatar answered Nov 01 '22 03:11

DreadPirateShawn