Created
December 28, 2011 07:23
-
-
Save matenia/1526936 to your computer and use it in GitHub Desktop.
Dynamic page routes with friendly_id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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