基于子域的来自不同路径的 Rails 3.1 负载控制器 [英] Rails 3.1 load controller from different path based on subdomain

查看:29
本文介绍了基于子域的来自不同路径的 Rails 3.1 负载控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以动态更改使用控制器的路径?Ryan Bates 在此处展示了如何更改 view_paths:http://railscasts.com/episodes/269-template-继承

Is it possible to dynamically change the path from which controllers are used? Ryan Bates showed how to change the view_paths here: http://railscasts.com/episodes/269-template-inheritance

我正在制作一个 CMS,用户可以在其中创建网站并输入他们自己的子域.如果没有子域,我希望/"指向public#welcome",但如果有子域,我希望它指向sites/public#welcome".

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".

如果有什么不同,我使用的是 Rails 3.1.

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

推荐答案

我想通了:

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

  root :to => 'public#welcome'

现在,当用户访问/"时,如果存在子域,将使用 Sites::PublicController,但如果没有子域存在,则仅使用 PublicController.添加 scope :module =>"sites" do...end 使我的路由文件保持简单和易于管理.

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.

这篇关于基于子域的来自不同路径的 Rails 3.1 负载控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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