Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails I18n区域设置路由和RSpec测试_Ruby On Rails_Internationalization_Rspec_Rspec2 - Fatal编程技术网

Ruby on rails Rails I18n区域设置路由和RSpec测试

Ruby on rails Rails I18n区域设置路由和RSpec测试,ruby-on-rails,internationalization,rspec,rspec2,Ruby On Rails,Internationalization,Rspec,Rspec2,我的申请书是用英语写的,一切都很好。昨天我开始玩Rails.I18n国际化支持。一切都很好。当我浏览http://localhost:3000/jp/discounts它是日文的,而且http://localhost:3000/discounts'为我提供默认的英语语言环境(未指定语言环境时) 这是my route.rb,如您所见,管理员命名空间未本地化: scope '(:locale)' do resources :discounts do resource :map, only

我的申请书是用英语写的,一切都很好。昨天我开始玩Rails.I18n国际化支持。一切都很好。当我浏览
http://localhost:3000/jp/discounts
它是日文的,而且http://localhost:3000/discounts'为我提供默认的英语语言环境(未指定语言环境时)

这是my route.rb,如您所见,管理员命名空间未本地化:

scope '(:locale)' do
  resources :discounts do
    resource :map, only: :show
    collection do
      get :featured_city
    end
  end
end
namespace :admin do
  resources :users do
    collection do
      get :members
      get :search
    end
  end
end
然而,我的RSpec开始失败

Failure/Error: it { should route_to('admin/users#edit', id: '1') }
The recognized options <{"action"=>"edit", "controller"=>"users", "locale"=>"admin", "id"=>"1"}> 
  did not match <{"id"=>"1", "controller"=>"admin/users", "action"=>"edit"}>, 
difference: <{"controller"=>"admin/users", "locale"=>"admin"}>.
  <{"id"=>"1", "controller"=>"admin/users", "action"=>"edit"}> expected but was
  <{"action"=>"edit", "controller"=>"users", "locale"=>"admin", "id"=>"1"}>
config/initializers/i18n.rb
当Rails尝试将给定URL与路由匹配时,它从
config/routes.rb
文件的顶部开始,并在它认为匹配的第一条路由处停止。因为,在您最初的问题中,您首先使用了
范围
块,Rails认为您的
/admin
URL指示了带有
:locale=>“admin”
的路由

您需要Rails将从
/admin
开始的路径匹配到您的管理员命名空间。通过将其放在routes文件中的第一个位置,Rails一旦找到匹配项,就会“停止查找”

这是一个严重的过分简化,但我希望它是有益的


如果还没有,也请查看。

如果将
命名空间:admin
块移动到
范围
块上方,会发生什么情况?谢谢!它消除了大部分错误(剩下的是另一个问题)。但我不明白这背后的机制,我的范围是如何影响它之外的东西的?你能写下答案并解释一下吗?
  def default_url_options
    { locale: I18n.locale }
  end
#encoding: utf-8
I18n.default_locale = :en

LANGUAGES = [
  ['English', 'en'],
  ["Japanese", 'jp']
]