SirRawlins
SirRawlins

Reputation: 494

Routing specs and subdomain constraint

I have a route which is constrained to only run when a subdomain is present, which looks something like this:

# Set the default root for when a subdomain is used.
match '', to: 'public::pages#show', constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'app' }    

# Set the default application route.
root :to => "pages#index"

I'm looking to put together some routing specs to test that this routes properly (testing the app manually shows that the route does in fact work just fine) however I'm coming up against a few issues.

My current shot at a routing test for this is simply to supply the entire domain, with subdomain as part of the get() call:

it "routes to public page on subdomain" do
  get('http://somesubdomain.example.com/').should route_to("public::pages#show")
end

However this spec fails, where the router has directed to the root event, rather than the intended one.

<{"controller"=>"public::pages", "action"=>"show"}> expected but was
   <{"controller"=>"pages", "action"=>"index"}>.

Can anyone suggest why this might not be working?

Upvotes: 1

Views: 389

Answers (1)

Stuart M
Stuart M

Reputation: 11588

Sounds like your root :to => "pages#index" route is taking precedence, and the match ''... route is getting ignored. I would try two root routes and set your :constraint lambda on the first one:

# Set the default root for when a subdomain is used.
root to: 'public::pages#show',
  constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'app' }    

# Set the default application route.
root :to => "pages#index"

See this blog post for more details about using constraints on your root route.

Upvotes: 1

Related Questions