How to set defaults for parameters of url helper methods?

后端 未结 3 1477
盖世英雄少女心
盖世英雄少女心 2021-02-06 07:15

I use language code as a prefix, e.g. www.mydomain.com/en/posts/1. This is what I did in routes.rb:

scope \":lang\" do
  resources :posts
end
         


        
3条回答
  •  执笔经年
    2021-02-06 08:10

    Ryan Bates covered this in todays railscast: http://railscasts.com/episodes/138-i18n-revised

    You find the source for url_for here: http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html

    You will see it merges the given options with url_options, which in turn calls default_url_options.

    Add the following as private methods to your application_controller.rb and you should be set.

    def locale_from_cookie
      # retrieve the locale
    end
    
    def default_url_options(options = {})
      {:lang => locale_from_cookie}
    end
    

提交回复
热议问题