【问题标题】:Rails routes: different domains to different placesRails 路线:不同的域到不同的地方
【发布时间】:2013-08-27 03:46:19
【问题描述】:

我在服务器上运行了一个 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 可以处理这个问题吗?谢谢!

【问题讨论】:

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


    【解决方案1】:

    您可以通过覆盖应用程序控制器中的默认 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
    

    为了将路由指向某个特定的 url,您可以使用:

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

    更好的方法是在服务器上进行设置以将某些 url 重定向到特定位置。

    【讨论】:

      【解决方案2】:

      它会根据某种标准转到/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
      

      【讨论】:

      • 嗯,是的,我可以检查控制器中的域。这似乎可以通过routes.rb 更好地管理。
      猜你喜欢
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多