Rails 3 render xml regardless of request.format

I am trying to create an API and I need to return xml regardless of the request format. I now have the following in my controller:

def index @posts = Post.all respond_to do |format| format.xml end end 

I have index.xml.builder

'/posts.xml' works for me, but not '/ posts'

I tried request.format =: xml, which gave me a SystemStackError (the stack level is too deep) :. Why is this happening.

How to make the controller display xml for all types of requests? so I donโ€™t need to specify a format in the url that looks clean and neat?

+4
source share
2 answers
 def index @posts = Post.all respond_to do |format| format.any do render :xml => @posts.to_xml end end end 
+2
source

I liked this since I had a customized xml view for rendering:

 def index respond_to do |format| format.any do headers["Content-Type"] = "application/xml; charset=utf-8" render "index.xml" end end end 
0
source

Source: https://habr.com/ru/post/1335508/


All Articles