Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing Subdomains and Removing Subdomains

I am writing an app that will use basecamp style subdomains (See post).

There are portions of the app that should always be in a subdomain and portions that should never be in a subdomain. For example, the "home" page should never be in a subdomain, but the blogs and posts resources always should. Keep in mind, I don't know the subdomains, as there is one per account.

I set up my routes like this (example). It works well in that it matches the routes, but I am having difficulty forcing my app to link to the correct locations.

MyApp::Application.routes.draw do

  # these should all route to mysubdomain.myapp.com
  # Clicking on a post from myapp.com/categories/12 should take you to mysubdomain.myapp.com/posts/14
  constraints(Subdomain) do
    resources :blogs { resources :posts }
  end

  # these should all route to myapp.com (clicking on one of these links from mysubdomain.myapp.com should go to myapp.com/categories/12)
  constraints(NoSubDomain) do
    resources :categories
  end

end

The problem is that once I am in a subdomain, all of the links to the other pages (categories, etc) contain the subdomain. I am not sure the best to solve this issue. Thank you so much for your help.

like image 325
Brandon Hansen Avatar asked Apr 18 '11 23:04

Brandon Hansen


1 Answers

It looks like could be done by overriding the url_for method to include a :subdomain option.

See the Cleaning Up The Code To Change The Subdomain section of RailsCasts #221.

Railscasts: http://railscasts.com/episodes/221-subdomains-in-rails-3

ASCIIcasts: http://asciicasts.com/episodes/221-subdomains-in-rails-3

Then you could do something like:

<p><%= link_to "All Blogs", root_url(:subdomain => false) %></p>  
like image 78
talyric Avatar answered Oct 12 '22 23:10

talyric