启动轨道I18n和url助手似乎将区域设置与id混淆

我刚刚开始尝试更新我们的rails 3.2应用程序以实现国际化。 我在路线中有一个可选的范围,比如

范围“(:locale)”,locale:/ en | es | zh-HK | de | fr / do

如http://guides.rubyonrails.org/i18n.html所述 。

我的url助手就像

watch_url(@watch)

在表单中获取错误

没有路由匹配{:action =>“show”,:controller =>“watches”,:locale =>#”“,”收件人“ =>“”,“attributes”=>“”},状态:{},隐藏:false,评论:“”>}

关于它为什么会发生以及我如何解决它的任何想法?

谢谢!

编辑:

在我的应用程序控制器中,我有

before_filter :set_locale def set_locale I18n.locale = params[:locale] || I18n.default_locale end 

和手表控制器的路线(这是一个8岁的专有应用程序,我们有100多个控制器:(,所以我不会包括所有路线)…

 $ rake routes | grep watch watches GET (/:locale)/watches(.:format) watches#index {:locale=>/en|es|zh-HK|de|fr/} POST (/:locale)/watches(.:format) watches#create {:locale=>/en|es|zh-HK|de|fr/} new_watch GET (/:locale)/watches/new(.:format) watches#new {:locale=>/en|es|zh-HK|de|fr/} edit_watch GET (/:locale)/watches/:id/edit(.:format) watches#edit {:locale=>/en|es|zh-HK|de|fr/} watch GET (/:locale)/watches/:id(.:format) watches#show {:locale=>/en|es|zh-HK|de|fr/} PUT (/:locale)/watches/:id(.:format) watches#update {:locale=>/en|es|zh-HK|de|fr/} DELETE (/:locale)/watches/:id(.:format) watches#destroy {:locale=>/en|es|zh-HK|de|fr/} 

我不认为show方法是相关的。 错误发生在url帮助器中,并且流程永远不会到达show代码。

您需要为url helper提供一个locale选项 ,以便正确传递它:

watch_url(@watch, locale: I18n.locale)

如果这有点烦人,你可以覆盖Rails url_options方法,总是给每个请求一个:locale选项,这样你就不必在任何视图中指定它了(一些指南,包括上面的Rails指南链接,比如覆盖default_url_options方法,但这对我不起作用……):

应用程序/控制器/ application_controller.rb

 class ApplicationController < ActionController::Base # Every helper method dependent on url_for (eg helpers for named # routes like root_path or root_url, resource routes like books_path # or books_url, etc.) will now automatically include the locale in # the query string, def url_options { locale: I18n.locale }.merge(super) end # ... end 

应用/视图/ your_view

 # locale not needed explicitly as it's set in the controller <%= link_to watch_url(@watch) %>