Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override `root_path` in Rails?

I have locales defined like this:

# routes.rb
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
  ...
  root "home#index"
end

I also set locale in default_url_params in ApplicationController.
This way I have all my routes and urls to include locales like this: http://example.com/en/something
http://example.com/fr/something
It is an expected behavior for me.

I also have root urls like this:
http://example.com/en
http://example.com/fr

What I want to achieve is exclude en (default) locale for root_path, so that root_path for :en locale (either passed explicitly or via default_url_params) is http://example.com/

Is there any way to do it? Can I maybe override root_path to do my hacking inside it?

Thank you.

like image 925
konnigun Avatar asked Oct 31 '25 01:10

konnigun


1 Answers

override root_path method in your ApplicationController:

def root_path
  (I18n.locale == 'en') ? '/' : super
end
like image 116
asiniy Avatar answered Nov 02 '25 22:11

asiniy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!