Lee*_*ley 15 ruby ruby-on-rails-3
我正在使用以下路由条件的Rails 3.2应用程序:
scope "(:locale)", locale: /de|en/ do
resources :categories, only: [:index, :show]
get "newest/index", as: :newest
end
Run Code Online (Sandbox Code Playgroud)
我有一个控制器,具有以下内容:
class LocaleController < ApplicationController
def set
session[:locale_override] = params[:locale]
redirect_to params[:return_to]
end
end
Run Code Online (Sandbox Code Playgroud)
我在模板中使用这样的东西:
= link_to set_locale_path(locale: :de, return_to: current_path(locale: :de)) do
= image_tag 'de.png', style: 'vertical-align: middle'
= t('.languages.german')
Run Code Online (Sandbox Code Playgroud)
我想知道为什么在Rails中不存在帮助器,例如current_path,能够推断出我们当前使用的路由,并且重新路由到它包括新选项.
我遇到的问题是使用类似的东西redirect_to :back,一个将用户推回/en/........(或/de/...),这会带来糟糕的体验.
到目前为止,我在会话中存储了区域设置,但这不适用于Google和其他索引服务.
我敢肯定,如果我投入足够的时间,我可以用一些足够聪明的东西来检测匹配的路线,并交换出语言环境部分,但我觉得这将是一个黑客.
我对所有想法持开放态度,但这个问题建议只使用sub(); 不幸的是,有这么短且频繁出现的字符串作为语言环境短代码,可能不太明智.
ibl*_*lue 30
如果您使用的:locale范围,你可以使用url_for如current_path:
# Given your page is /en/category/newest with :locale set to 'en' by scope
url_for(:locale => :en) # => /en/category/newest
url_for(:locale => :de) # => /de/kategorie/neueste
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15375 次 |
| 最近记录: |