Rails路由:不同的域到不同的地方

时间:2013-08-27 03:46:20

标签: ruby-on-rails ruby ruby-on-rails-3 routing routes

我在服务器上运行了Rails应用程序。这是一个很大的项目,所以涉及很多路线,目前有两个域指向根。我想以某种方式设计我的routes.rb来解释一个域,将它带到应用程序的某个部分,就好像它是根,并将其他域用于其他地方。

像这样的东西(非常伪代码,希望你明白这个想法):

whole_app.com
whole_app.com/documents
whole_app.com/share
whole_app.com/users
partial_app.com, :points_to => 'whole_app.com/share'

Rails可以处理吗?感谢您!

2 个答案:

答案 0 :(得分:2)

您可以通过覆盖应用程序控制器中的默认url_options方法来实现此目的。这将覆盖每个请求的主机URL。

class ApplicationController < ActionController::Base
  ....
  def default_url_options
    if some_condition
      {:host => "partial_app.com"}
    else  
      {:host => "whole_app.com"}
    end
  end
  ....
end

要将路线指向某个特定网址,您可以使用:

match "/my_url" => redirect("http://google.com/"), :as => :my_url_path

更好的方法是在服务器上进行设置,将某个网址重定向到特定位置。

答案 1 :(得分:0)

是否会根据某种标准转到/share?如果是这样,你可以这样做:

routes.rb

root :to => 'pages#home'

pages_controller.rb

def home
  if (some condition is met) 
    redirect_to this_path
  else
    render :layout => 'that'
  end
end
相关问题