启动轨道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) %>
- Rails使用Twitter Bootstrap设计I18n Flash消息
- Ruby on Rails 3.2.13 – 使用I18n获取自定义错误页面的错误
- 全局化rails中的文件(附件,图像)
- I18n locale无视后备
- 动态添加到I18N的翻译
- Way Rails不会引发I18n :: MissingInterpolationArgumentexception?
- Rails:Model.human_attribute_name:字段应该在找不到翻译时引发错误? (也许是由state_machine引起的?)
- bundler安装获取“i18n需要Ruby版本> = 1.9.3”
- 用I18n.t提交按钮助手