【问题标题】:How to remove subdomain from root path in Rails如何从 Rails 的根路径中删除子域
【发布时间】:2015-02-28 11:50:28
【问题描述】:

我想从根路径中删除子域。

我尝试将:subdomain => false 添加到routes.rb 文件中的root 命令但没有成功:当我在URL 中手动输入子域时,子域会保留并且不会被删除。

例子:

my root is => lvh.me:3000
enter subdomain manually => xyz.lvh.me:3000 and hit enter then it remains the same

这是我在routes.rb 文件中已经尝试过的,但没有成功:

root :to => 'home#show', :subdomain => false or
root :to => 'home#show', :constraints => { :subdomain => false }, via: [:get]

【问题讨论】:

    标签: ruby-on-rails subdomain


    【解决方案1】:

    将其写入应用程序控制器。这会起作用。

    before_action :check_subdomain
    
    def check_subdomain
      unless company_signed_in?
        if request.subdomain.present? && params[:controller] == "companies/registrations" && params[:action] == "new"
          redirect_to root_url, subdomain: false
       end
      end
    end
    

    【讨论】:

      【解决方案2】:

      根据this comment on rails github,在Rails >= 4 中,您需要使用约束来获得它。试试这个:

      constraints subdomain: false do
        root to: 'home#show'
      end
      

      【讨论】:

        【解决方案3】:

        @dgilperez:您的代码运行良好,但我也需要更改应用程序控制器,是的,我找到了解决方案,我刚刚更新了

        before_action :check_subdomain
        
        def check_subdomain
          unless company_signed_in?
            if request.subdomain.present? && params[:controller] == "companies/registrations" && params[:action] == "new"
              redirect_to root_url, subdomain: false
            end
          end
        end
        

        【讨论】:

          猜你喜欢
          • 2015-02-11
          • 1970-01-01
          • 1970-01-01
          • 2015-10-01
          • 1970-01-01
          • 2010-10-16
          • 2013-12-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多