I know this has been asked already, but still I can’t make it work.
I’m trying to get the :locale parameter to show by using default_url_options, like this:
# SomethingController < ApplicationController
def default_url_options(options={})
{ :locale => I18n.locale }
end
I18n.locale is set with a before_filter and has the correct value (debugged with pry).
The routes:
# In routes
scope "(:locale)", :defaults => { :locale => I18n.default_locale } do
get "something" => "something#index", :as => :something_index
end
In my view I tried:
url_for :something_index
and
something_index_url
but they both return “/something” instead of “/en/something”. Is there something else I have to do in order to automatically get the parameter to show?
I read about this here:
Every helper method dependent on url_for (e.g. helpers for named
routes like root_path or root_url, resource routes like books_path or
books_url, etc.) will now automatically include the locale in the
query string
You could amend the get matcher:
For a better method, refer here:
Rails optional /:locale route