Skip to content

Instantly share code, notes, and snippets.

@tejo
Forked from pixeltrix/routes.rb
Created April 1, 2011 21:41
Show Gist options
  • Save tejo/898914 to your computer and use it in GitHub Desktop.
Save tejo/898914 to your computer and use it in GitHub Desktop.
Examples of advanced Rails Routes
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
Please use this in you routes.rb:
scope "(/:locale)" do
resources :items
end
and in your application controller:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale
def set_locale
I18n.locale = params[:locale]
end
def default_url_options(options={})
{:locale => I18n.locale}
end
end
config/application.rb:
config.i18n.default_locale = :de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment