开发者

rails 3 render xml irrespective of request.format

开发者 https://www.devze.com 2023-02-04 12:53 出处:网络
I am trying to create an API and i need to return xml irrespective of the request format. now i have the following in my controller

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

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

i have a index.xml.builder

'/posts.xml' works fo开发者_StackOverflow社区r me but not '/posts'

i tried request.format = :xml which gave me a SystemStackError (stack level too deep):. why is it happening.

How can i force the controller to render xml for all types of request ? so that i dont need to specify the format in the url which looks clean and tidy?


def index
  @posts = Post.all
  respond_to do |format|
    format.any do
      render :xml => @posts.to_xml
    end
  end
end


I did like this since I had a custom xml view to render:

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

精彩评论

暂无评论...
验证码 换一张
取 消