motoki
motoki

Reputation: 1

Rails 3 - How to escape from subdomain route to the normal route?

I've worked on making a multiple subdomains feature for my E-commerce website. So far, I've made it possible to assign a ':subdomain' attribute for every user and display their sites on like 'subdomain.example.com'.

But, I cannot return to 'example.com' from 'subdomain.example.com' because the 'root_path' leads to not 'example.com' but 'subdomain.example.com'.

routes.rb file:

constraints(Subdomain) do
  match "/" => 'contributors#show'
end

root :to => "items#index"

The Subdomain class comes from 'domains.rb' file below.

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != "www"
  end
end

contributors#show

<%= link_to 'Home', root_path # this leads to contributor#show. wanna make it to index#show. %>

Any help is welcome. Thanks.

Upvotes: 0

Views: 623

Answers (2)

jfdimark
jfdimark

Reputation: 2369

Have you tried root_url(:subdomain => false)? This strips the link of the subdomain and should do what you want.

Upvotes: 2

bubblez
bubblez

Reputation: 814

Rails takes the first matching path defined in the routes.rb file. If you are on the subdomain the constraint matches and match "/" matches as root path. Try move "root :to => 'items#index'" to the top of your routes.rb file.

Upvotes: 0

Related Questions