特定のサブドメインに制限されている次のルートがあるとします。
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_url
、backend_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") を使用して正しい結果を得ることができます。ただし、コード全体でこれを指定する必要はありません。