2

我知道这已经被问过了,但我仍然无法让它发挥作用。我正在尝试使用 获取:locale要显示的参数default_url_options,如下所示:

# SomethingController < ApplicationController
def default_url_options(options={})
  { :locale => I18n.locale }
end

I18n.locale用 a 设置before_filter并具有正确的值(用 pry 调试)。路线:

# In routes
scope "(:locale)", :defaults => { :locale => I18n.default_locale } do
  get "something" => "something#index", :as => :something_index
end

在我看来,我尝试过:

url_for :something_index

something_index_url

但它们都返回“/something”而不是“/en/something”。为了自动显示参数,我还需要做些什么吗?

我在这里读到了这个:

每个依赖于 url_for 的辅助方法(例如,命名路由的辅助方法,如 root_path 或 root_url,资源路由如 books_path 或 books_url 等)现在将自动在查询字符串中包含语言环境

4

1 回答 1

0

您可以修改 get 匹配器:

scope :locale, :defaults => { :locale => I18n.default_locale } do
  get "/:locale/something" => "something#index", :as => :something_index
end

如需更好的方法,请参阅此处:

Rails 可选 /:locale 路由

于 2012-11-15T11:44:49.477 回答