如何从 Rails 中的根路径中删除子域

How to remove subdomain from root path in Rails

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

我尝试将 :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]

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

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

@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

在应用程序控制器中写入。这会起作用。

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