开发者

Rails 3.1 load controller from different path based on subdomain

Is it possible to dynamically change the path from which controll开发者_C百科ers are used? Ryan Bates showed how to change the view_paths here: http://railscasts.com/episodes/269-template-inheritance

I'm making a CMS where a user can create a site and enter their own subdomain. I'd like "/" to point to "public#welcome" if there's no subdomain, but if there is a subdomain, I want it to point to "sites/public#welcome".

I'm using Rails 3.1 if that makes any difference.


You should be able to solve this situation using constraints if I'm not mistaken (which I might, since I haven't actually tried the following yet):

constraints(:subdomain => /.+/) do
  root :to => 'sites/public#welcome'
end

root :to => 'public#welcome'


I figured it out:

  constraints(:subdomain => /.+/) do
    scope :module => "sites" do
      root :to => 'public#welcome'
    end
  end

  root :to => 'public#welcome'

Now when a user visits "/" Sites::PublicController will be used if a subdomain exists, but just PublicController if no subdomain exits. Adding scope :module => "sites" do...end keeps my routes file simplistic and manageable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜