围绕`mount`的`scope`无效?

时间:2017-07-06 09:24:06

标签: ruby-on-rails internationalization url-routing blacklight

(虽然这讨论了Blacklight引擎,但我相信这个问题实际上只是关于Rails。)

我正在尝试将国际化添加到我的Blacklight应用中。为此,我

  • inherit extrausers EXTRA_USERS_PARAMS = " useradd user1; \ useradd user2; \ useradd user3; \ usermod -p 'user1_psw' user1; \ usermod -p 'user2_psw' user2; \ usermod -p 'user3_psw' user3;\ usermod -a -G sudo user1; \ usermod -a -G sudo user2; \ usermod -a -G sudo user3; " 中的所有内容包装到config/routes.rb,然后

  • scope "(:locale)", locale: /en|ja/
  • 我添加了app/controllers/application_controller.rb并覆盖了before_action :set_locale

Rails i18n guide建议。大多数事情都有效,但有一点我无法弄清楚。

我的所有应用程序路由都已正确映射,例如default_url_optionshttp://www.example.com/en/catalog/12345正确匹配,并使用(/:locale)/catalog/:id(.:format)路由到catalog#show。所有Devise的网址都很好。一切正常......除了{:id=>/[^\/]+(?=\.json|\.html|$|\/)/, :locale=>/en|ja/} - 编辑的Blacklight引擎。

显然,Blacklight引擎不会收听mountscope显示:

rake routes
我希望

而不是Routes for Blacklight::Engine: search_history GET /search_history(.:format) search_history#index ....

我修改了Blacklight模板,以便我用日语和英语指向当前页面的语言选择器,但是当我导航到(:locale)/search_history(.:format)时,search_history在面对时突然抛出url_for参数。

为什么:locale会忽略mount?如何解决我的问题(引擎路由也响应scope)?

这是我使用:locale修改的默认Blacklight生成的config/routes.rb

scope

tl; dr: Rails.application.routes.draw do scope "(:locale)", locale: /en|ja/ do mount Blacklight::Engine => '/' root to: "catalog#index" concern :searchable, Blacklight::Routes::Searchable.new resource :catalog, only: [:index], as: 'catalog', path: '/catalog', controller: 'catalog', id: /[^\/]+(?=\.json|\.html|$|\/)/ do concerns :searchable end devise_for :users concern :exportable, Blacklight::Routes::Exportable.new resources :solr_documents, only: [:show], path: '/catalog', controller: 'catalog', id: /[^\/]+(?=\.json|\.html|$|\/)/ do concerns :exportable end resources :bookmarks, id: /[^\/]+(?=\.json|\.html|$|\/)/ do concerns :exportable collection do delete 'clear' end end end end 为我的所有路由添加前缀,但scope除了路由。为什么以及如何解决?

1 个答案:

答案 0 :(得分:1)

看起来Rails确实忽略了引擎路由中的scope,但我可以明确地为引擎添加一个范围:

Blacklight::Engine.routes.default_scope = { path: "(:locale)", locale: /en|ja/ }
mount Blacklight::Engine => '/'

但是,这仍然无法解决我的问题(继续https://www.postgresql.org/docs/current/static/functions-json.html)。