Skip to content

Instantly share code, notes, and snippets.

@adambair
Created February 1, 2011 18:10
Show Gist options
  • Select an option

  • Save adambair/806297 to your computer and use it in GitHub Desktop.

Select an option

Save adambair/806297 to your computer and use it in GitHub Desktop.
class ProductObserver < ActiveRecord::Observer
observe :product
def after_save(product)
reload_routes
end
def after_destroy(product)
reload_routes
end
def reload_routes
ActionController::Routing::Routes.reload!
end
end
ActionController::Routing::Routes.draw do |map|
# Products
map.products '/products', :controller => 'products', :action => 'index'
# AB: the filter controls the tab navigation on the /products page
map.product_filter '/products/:filter', :controller => 'products', :action => 'index',
:requirements => {:filter => %r[featured|enterprise|mobile|all]}
# AB: Normally a bad idea (routes are not updated in prod mode)
# I'm currently using an observer to update the routes when a new product is added.
# This prevents routing errors when adding additional products or when changing slugs
products = Product.enabled.collect{|product| product.slug }
map.product '/:id', :controller => 'products', :action => 'show',
:requirements => {:id => %r[#{products.join("|")}]}
map.product_page '/:id/:page_id', :controller => 'products', :action => 'show',
:requirements => {:id => %r[#{products.join("|")}]}
end
@adambair
Copy link
Copy Markdown
Author

adambair commented Feb 1, 2011

We have multiple base level <domain.com>/:something pointing to different controllers and this is one possible way to work around that need. Though this might not be the best solution - it seems to work okay. Keep in mind that this for Rails 2.3.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment