将顶级域路由(或重定向)到rails app的子目录

时间:2016-10-18 16:27:59

标签: ruby-on-rails ruby .htaccess ruby-on-rails-4 rails-routing

我有一个rails应用程序,它根据查看当前子域的约束来安装几个引擎中的一个。我还想包括一个约束,它查看当前的顶级域,然后在特定的子目录中安装其中一个引擎;例如www.example.com指向subdirectory.railsapp.com/example

这是我当前的routes.rb

Rails.application.routes.draw do
  mount Accounts::Engine, at: "/"

  constraints subdomain: "campaigns" do
    mount Petitions::Engine, at: "/"
  end

  constraints subdomain: "blog" do
    mount Blog::Engine, at: "/"
  end

  constraints subdomain: "events" do
    mount Calendar::Engine, at: "/"
  end

end

我想添加一个条件,如果域名为www.example.com,则执行类似mount Petitions::Engine, at: "/example"的操作。

1 个答案:

答案 0 :(得分:1)

您可以使用lambda / proc来实现高级约束(http://guides.rubyonrails.org/routing.html#advanced-constraints):

get '/example', to: 'controller#action',
    constraints: lambda { |request| request.host.match /example/ }

request与您在控制器中获得的request.env相同。您可以查看request.remote_ipconstraints(lambda { |request| request.host.match /example/ }) do mount Petitions::Engine, at: "/example" end 等。

更多文档:http://apidock.com/rails/v3.2.1/ActionDispatch/Routing/Mapper/Scoping/constraints

对于您的情况,您可以这样做:

t1 = [1476369169.1, 1476369169.2, 1476369169.3, 1476369169.4, 1476369169.5];
TableA = table(t1', [1, 2, 3, 4, 5]', 'VariableNames', {'Time', 'A'});

t2 = [1476369169.1, 1476369169.3, 1476369169.4, 1476369169.5];
PreciseTableB = table(t2', [1, 3, 4, 5]', 'VariableNames', {'Time', 'B'});

PreciseJoin = outerjoin(TableA,PreciseTableB, 'Keys', 'Time', 'MergeKeys', 1)

t4 = t2 + rand(1, 4) / 100;
ErrorTableB = table(t4', [1, 3, 4, 5]', 'VariableNames', {'Time', 'B'});
ErrorJoin = outerjoin(TableA,ErrorTableB, 'Keys', 'Time', 'MergeKeys', 1)