Rendering SVG with respond_to

Hi you all, I've used the library SVG::Graph to generate SVG images. I have this code in the controller:

respond_to do |format|       format.html       format.xml { render :xml => @graphic.to_svg}     end

When html, I display a jpg graph. When XML, I make the graphic with SVG::Graph and the xml appears.

To see this SVG graph, I can store the resulting XML file from my browser, renaming it with the .svg extension, and clicking on the resulting file (I have the adobe plugin to see SVG images).

This is tedious, and this is why I've added the following in the controller: format.svg { render :xml => @graphic.to_svg}

And the following in environment.rb: Mime::Type.register "image/svg+xml", :svg

But this does not display the image directly; it stills renders an XML file. What should I do? Thanks.

I believe you'll need to set the content type header so that the browser knows you're serving svg:

format.xml do     headers["Content-Type"] = "'image/svg+xml; charset=utf-8"     render :xml => @graphic.to_svg end

That might need to be "@headers" and you may need to leave out "; charset=utf-8"

Best regards

Peter De Berdt

Hi Peter, no, it does not work. I've added: headers["content-type"] = "image/svg+xml"

and what happens is that when accesing, I received a message if I want to open or save the file "graphic.svg" (it is not displayed directly in the browser). It does not matter what I do, but the file is never open or saved, cause it does not exist ( because it is not a file stored in the server, i'm afraid).

Peter De Berdt wrote:

I got it!

I've used the method for sending other data, so: format.svg { send_data(@graphic.to_svg, :type=>"image/svg+xml", :disposition =>"inline") }

Thanks.

Damaris Fuentes wrote: