Mujtaba Alboori
Mujtaba Alboori

Reputation: 395

prevent default_url_options from being called and add paramters

I've default_url_options that set locale like that

  def default_url_options
      {
        locale: I18n.locale,

      }
  end

but in one case I want to redirect_to something_url without adding ?locale=en

currently direct_to something automatically add locale like this http://localhost/something?locale=en I want to remove the locale params

Upvotes: 0

Views: 131

Answers (3)

Ruturaj Bisure
Ruturaj Bisure

Reputation: 159

In above snippet simply add,

def default_url_options
   I18n.locale = (url == something_url) ? nil  : I18n.default_locale
   {
    locale: I18n.locale
    }
end

here url and something_url you need to add as per your application.

Hope this will work in your case.

Upvotes: 0

chumakoff
chumakoff

Reputation: 7024

You can prevent adding default url options by explicitly setting them to nil in the url helper:

redirect_to something_url(locale: nil)

Upvotes: 2

max
max

Reputation: 101811

Provided that en is the default locale you can do:

def default_url_options
  {}.tap do |options|
    if I18n.locale != I18n.default_locale 
      options[:locale] = I18n.locale 
    end
  end 
end

Upvotes: 0

Related Questions