Created
October 29, 2010 13:21
-
-
Save pixeltrix/653543 to your computer and use it in GitHub Desktop.
Examples of advanced Rails 3.0 routes
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
Rails.application.routes.draw do | |
get '/(:locale)/products/(:category)/(page/:page).:extension', | |
:to => 'products#index', | |
:as => :products, | |
:constraints => { | |
:locale => /[a-z]{2}/, | |
:category => /.+?/, | |
:page => /\d+/ | |
}, | |
:defaults => { | |
:page => 1, | |
:extension => 'html', | |
:locale => 'en' | |
} | |
# products_path(:page => 1) | |
# => /products.html | |
# products_path(:page => 2) | |
# => /products/page/2.html | |
# products_path(:page => 2, :locale => 'de') | |
# => /de/products/page/2.html | |
# products_path('computers', :page => 2, :locale => 'de') | |
# => /de/products/computers/page/2.html | |
# products_path('computers/apple', :page => 2, :locale => 'de') | |
# => /de/products/computers/apple/page/2.html | |
# products_path('computers/apple') | |
# => /products/computers/apple.html | |
end |
Worked like a charm! thank you
So when you write:
products_path('computers', :page => 2, :locale => 'de')
How does the method know that computers is the category when its not being specified like the other params?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ifightcrime you can set a default url option for the
:locale
key in a before filter or override thedefault_url_options
method - see this Stack Overflow answer for an example.