Created
February 1, 2011 18:10
-
-
Save adambair/806297 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.