11

特定のサブドメインに制限されている次のルートがあるとします。

App::Application.routes.draw do
  constraints :subdomain => "admin" do
    scope :module => "backend", :as => "backend" do
      resources :signups
      root :to => "signups#index"
    end
  end
  constraints :subdomain => "www" do
    resources :main
    root :to => "main#landing"
  end
end

私の問題はroot_urlbackend_root_url両方が現在のサブドメインの URL を返すことです:「http:// current-subdomain .lvh.me/」ではなく、リソースに固有のサブドメインです。root_url「http:// www .lvh.me/」backend_root_urlを返し、「http:// admin .lvh.me/」を返したいと思います (動作は、サブドメインの下のすべてのリソースで同じである必要があります)。

アプリケーションコントローラーの url_options など、さまざまな場所で url オプションを設定することにより、Rails 3.2 でこれを実現しようとしました。

class ApplicationController < ActionController::Base
  def url_options
    {host: "lvh.me", only_path: false}.merge(super)
  end
end

URLヘルパーを手動でオーバーライドする必要があるのでしょうか? どのようにアプローチしますか(ルートへのアクセスなど)?

編集:現在のサブドメインに関係なく、「http:// admin .lvh.me/」を返す root_url(:subdomain => "admin") を使用して正しい結果を得ることができます。ただし、コード全体でこれを指定する必要はありません。

4

1 に答える 1

10

以下に示すように「デフォルト」を使用すると、Rails url ヘルパーが正しいサブドメインを出力します。

App::Application.routes.draw do
  constraints :subdomain => "admin" do
    scope :module => "backend", :as => "backend" do
      defaults :subdomain => "admin" do
        resources :signups
        root :to => "signups#index", :subdomain => "admin"
      end
    end
  end

  constraints :subdomain => "www" do
    defaults :subdomain => "www" do
      resources :main
      root :to => "main#landing"
    end
  end
end
于 2012-05-09T17:06:53.127 に答える