heroxav
heroxav

Reputation: 1467

Rails: How to route to a specific domain

I have the following route defined:

constraints( subdomain: "abc" ) do
    resources :sites do
        resources :pages
    end
    resources :domains
end

So I can access a page like so: abc.example.com/sites/1/pages/2

I want to route only the show action of the PagesController to be accessible like this: a-different-domain.com/2

I have many domains and one domain can have many pages. Domains have the column domain. This is what I've thought would work:

%w(domains).each do |d|
    constraints( host: d.domain ) do
        get "/*structure" => "pages/dashboard/sites/pages#show"
    end
end

I am getting the following error:

undefined method `domain' for "domains":String (NoMethodError)

How can you do that with Rails?

Upvotes: 0

Views: 1521

Answers (1)

etagwerker
etagwerker

Reputation: 532

The problem with your code is that %w(domains) is ["domains"]

You could do this:

# routes.rb
Domain.find_each do |d|
  constraints(subdomain: d.domain) do
    get "/*structure" => "pages/dashboard/sites/pages#show"
  end
end

For more information: http://edgeguides.rubyonrails.org/routing.html#request-based-constraints

Upvotes: 1

Related Questions