如何根据域名更改视图格式 [英] Howto change view format based on domain name

查看:37
本文介绍了如何根据域名更改视图格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以根据域名更改同一 rails 应用程序的视图格式.

I wanted to know if there are any way to change the view format for based on domain name the same rails app.

例如:

  • www.domain.com => respond_to format.html
  • api.domain.com => respond_to format.xml 或 format.json

感谢大家的帮助

推荐答案

是的,在你的控制器中使用 before_filter 并根据 request.host<的值设置 response.format/代码>.

Yes, use a before_filter in your controller and set the response.format according to the value of request.host.

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

这篇关于如何根据域名更改视图格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆