开发者

Change request format based on subdomain in Rails 3

开发者 https://www.devze.com 2023-03-18 18:16 出处:网络
On my Rails 3 site, if a user comes in through a certain subdomain (say, mob.example.com), I want to change the request format of that request to be \":mobile\".

On my Rails 3 site, if a user comes in through a certain subdomain (say, mob.example.com), I want to change the request format of that request to be ":mobile".

What's the most sensible way to do this, 开发者_运维百科and where should I put this code?


I wound up doing this in the way I thought was most reasonable:

module MobilizedController
  extend ActiveSupport::Concern

  included do
    before_filter :set_mobile_request_format, :if => :mobile_subdomain?
  end 

  private

  def set_mobile_request_format
    request.format = :mobile
  end 

  def mobile_subdomain?
    request.subdomains.include? 'm' 
  end 
end

class ApplicationController
  include MobilizedController
end
0

精彩评论

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