-
-
Save anhphamt/15b6291f360cac6f55ef221a20015481 to your computer and use it in GitHub Desktop.
Examples of advanced Rails 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 | |
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