2012-09-26 11 views
13

¿Alguien me puede ayudar con la representación de plantilla XML y send_data?¿Cómo renderizar una plantilla XML y luego usar SEND_DATA en Ruby on Rails 3.2.8?

tengo un controlador:

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 

Pero tengo muchos errores con "nivel de pila demasiado profundo"

Si uso

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

consigo archivo XML, pero no de mi plantilla ...

EDITAR: ¡Tengo una forma!

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

¡Y todo funciona correctamente!

+0

Podría publicar toda su rastro? – sunnyrjuneja

+0

SystemStackError en CalculationsController # show nivel pila demasiado profundo Rails.root: C: /RubyStack-3.2.5-0/examplexml Aplicación de seguimiento | Seguimiento del marco | Traza completa actionpack (3.2.5) lib/action_dispatch/middleware/reloader.rb: 70 – Dmitry

+1

puedo tener un resultado en mi Chrome con: format.xml {render: format => false} Pero sólo la pantalla, no archivo para descargar – Dmitry

Respuesta

17

Copia de la respuesta del cuerpo cuestión editada con el fin de eliminar esta pregunta del filtro "sin respuesta":

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

~ respuesta por Dmitry