如何根据域名更改视图格式
我想知道是否有任何方法可以更改基于域名相同rails应用程序的视图格式。
例如 :
- www.domain.com => respond_to format.html
- api.domain.com => respond_to format.xml或format.json
感谢你的帮助
是的,在控制器中使用before_filter,并根据request.host
的值设置response.format
。
class Controller < ActionController::Base before_filter :adapt_response_format protected def adapt_response_format response.format = case request.host when "xml.foo.com" then :xml else :html end end
这是我猜测你的问题的另一种方法。
为什么不让客户根据他们想要的格式将Accept标头设置为application / xml或application / json? 您可以默认提供html以支持Web浏览器。
这样您就不需要拥有两个不同的主机。