Rails current_path Helper?

我正在使用以下路由条件的Rails 3.2应用程序:

scope "(:locale)", locale: /de|en/ do resources :categories, only: [:index, :show] get "newest/index", as: :newest end 

我有一个控制器,具有以下内容:

 class LocaleController < ApplicationController def set session[:locale_override] = params[:locale] redirect_to params[:return_to] end end 

我在模板中使用这样的东西:

  = 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') 

我想知道为什么在Rails中不存在帮助器,例如current_path ,这能够推断出我们当前使用的路由,并且重新路由到它包括新选项。

我遇到的问题是使用redirect_to :back ,将用户推回/en/........ (或/de/... ),这会带来糟糕的体验。

到目前为止,我在会话中存储了语言环境,但这不适用于Google和其他索引服务。

我敢肯定,如果我投入足够的时间,我可以使用足够聪明的东西来检测匹配的路线,并交换出语言环境部分,但我觉得这将是一个黑客。

我对所有想法持开放态度 ,但这个问题建议只使用sub() ; 不幸的是,有这么短且频繁出现的字符串作为语言环境短代码,可能不太明智。

如果您使用的是: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 

如果有人在这里看,你可以使用request.fullpath ,它应该为你提供所有的域名,因此,将包括locale。