14

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!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dmitry
  • 147
  • 1
  • 6
  • Could you post your entire trace? – sunnyrjuneja Sep 27 '12 at 08:04
  • SystemStackError in CalculationsController#show stack level too deep Rails.root: C:/RubyStack-3.2.5-0/examplexml Application Trace | Framework Trace | Full Trace actionpack (3.2.5) lib/action_dispatch/middleware/reloader.rb:70 – Dmitry Sep 27 '12 at 08:23
  • 1
    I can have a result in my Chrome with: format.xml { render :format=>false} But only screen, not file to download – Dmitry Sep 27 '12 at 08:24
  • Hey Dmitry, considering posting to your question and marking it as as solution. – sunnyrjuneja Sep 27 '12 at 17:30
  • @Dmitry - If you're game to recap the solution as your own answer, I'll delete my answer. (See http://meta.stackexchange.com/questions/90263/unanswered-question-answered-in-comments for elaboration of why this is helpful.) Thanks! – DreadPirateShawn Oct 09 '13 at 16:27

2 Answers2

20

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

Community
  • 1
  • 1
DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71
0

You can try this with Rails 5

respond_to do |format|
  format.html do
    stream = render_to_string(:template => "calculations/test.xml.builder")
    send_data stream,
              :type => 'text/xml; charset=UTF-8;',
              :disposition => "attachment; filename=test.xml"
  end
end
Plabon Asad
  • 1
  • 1
  • 2