Skip to content

Instantly share code, notes, and snippets.

@jo
Created March 30, 2010 05:42
Show Gist options
  • Save jo/348801 to your computer and use it in GitHub Desktop.
Save jo/348801 to your computer and use it in GitHub Desktop.
Route domains root to resources show
class Domain < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
validates :routable, :presence => true
belongs_to :routable, :polymorphic => true
end
class Post < ActiveRecord::Base
extend RouteByDomain
has_one :domain, :as => :routable
end
module RouteByDomain
def matches?(request)
return unless request.get?
domain = Domain.find_by_routable_type_and_name(self.to_s, request.host)
return unless domain
request.params[:id] = domain.routable_id
end
end
Rails3Routing::Application.routes.draw do |map|
constraints(Post) do
root :to => "events#show"
end
root :to => "welcome#index"
resources :posts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment