Skip to content

Instantly share code, notes, and snippets.

@matenia
Created December 28, 2011 07:23
Show Gist options
  • Save matenia/1526936 to your computer and use it in GitHub Desktop.
Save matenia/1526936 to your computer and use it in GitHub Desktop.
Dynamic page routes with friendly_id
#config/initializers/page_constraint.rb
class PageConstraint
def initialize
@page_paths = Page.all.map { |p| "/#{p.friendly_id}"}
end
def matches?(request)
@page_paths.include?(request.path)
end
end
# config/routes.rb
DummyApp::Application.routes.draw do
resources :pages, :path => '/', :only => :show, :constraints => PageConstraint.new
# if these are the other way around the show path won't be creted
resources :pages, :except => :show
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment