Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 81975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:32:39+00:00 2026-05-10T21:32:39+00:00

How can I go about making my routes recognise an optional prefix parameter as

  • 0

How can I go about making my routes recognise an optional prefix parameter as follows:

/*lang/controller/id 

In that the lang part is optional, and has a default value if it’s not specified in the URL:

/en/posts/1   => lang = en /fr/posts/1   => lang = fr /posts/1      => lang = en 

EDIT

Ideally, I’m looking to do this across many controllers and actions by mapping a namespace:

map.namespace '*lang' do |lang|   lang.resources :posts   lang.resources :stories end 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:32:40+00:00Added an answer on May 10, 2026 at 9:32 pm

    OK, I’ve managed to sort out this problem:

    THere is no way of doing this in Rails by default (at least, not yet). Instead of using namespaces and default values, I needed to install Sven Fuchs’ routing filter.

    Once the plugin is installed, I added the following file to my lib directory:

    require 'routing_filter/base'  module RoutingFilter   class Locale < Base      # remove the locale from the beginning of the path, pass the path     # to the given block and set it to the resulting params hash     def around_recognize(path, env, &block)       locale = nil       path.sub! %r(^/([a-zA-Z]{2})(?=/|$)) do locale = $1; '' end       returning yield do |params|         params[:locale] = locale || 'en'       end     end      def around_generate(*args, &block)       locale = args.extract_options!.delete(:locale) || 'en'       returning yield do |result|         if locale != 'en'           result.sub!(%r(^(http.?://[^/]*)?(.*))){ '#{$1}/#{locale}#{$2}' }         end        end     end    end end 

    I added this line to routes.rb:

    map.filter 'locale' 

    This basically fills out a before and after hook, generated by the plugin, that wraps the rails routing.

    When a url is recognised, and before Rails gets to do anything with it, the around_recognize method is called. This will extract a two-letter code representing the locale, and pass it through in the params, defaulting to ‘en’ if no locale is specified.

    Likewise, when a url is generated, the locale parameter will be pushed into the URL on the left side.

    This gives me the following urls and mappings:

    /   => :locale => 'en' /en => :locale => 'en' /fr => :locale => 'fr' 

    All existing url helpers work as before, with the only difference being that unless the locale is specified, it is preserved:

    home_path                  => / home_path(:locale => 'en') => / home_path(:locale => 'fr') => /fr 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.