I
I
Ivan Nesterovich2014-06-07 21:49:18
Ruby on Rails
Ivan Nesterovich, 2014-06-07 21:49:18

How to set default variable for url in rails

There is a need to pass the current language to each url. I found the default_url_options method in the docs, but it doesn't work. Rails version 4.1.
Tried like this in the controller:

helper_method :url_for
  def default_url_options
    { locale: "en" }
  end

and so in before_action:
Rails.application.routes.default_url_options[:locale]= 'en'

And even so in the configs:
config.action_controller.default_url_options = {:locale => "en"}

There is only one result - the locale parameter does not appear in the url. What's wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
yurnei, 2014-06-07
@yurnei

Maybe this will help rusrails.ru/rails-internationalization-i18n-api

D
Dimitriy, 2014-06-07
@Vakiliy

Is it registered somewhere in the marches?
Approximately like this

get '/posts(/:locale)'
# или
scope "/:locale" do
...

I
Ivan Nesterovich, 2014-06-07
@vanderv

I actually found the problem myself. The fact is that all my routes were wrapped with a scope of the form:

scope "(:locale)", :defaults => {:locale => "ru"}, :locale => /(ru|en)/ do

In this case, the locale was overwriting the one declared in default_url_options

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question