`mount`周围的`scope`无效? [英] `scope` around `mount` ineffective?

查看:88
本文介绍了`mount`周围的`scope`无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(尽管这里讨论的是Blacklight引擎,但我认为问题实际上纯粹是关于Rails的.)

(While this discusses Blacklight engine, I believe the question is actually purely about Rails.)

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

I am trying to add internationalisation to my Blacklight app. To that end, I

  • config/routes.rb中的所有内容包装到scope "(:locale)", locale: /en|ja/中,并且

  • wrapped everything in config/routes.rb into scope "(:locale)", locale: /en|ja/, and

我添加了before_action :set_locale并覆盖了default_url_options

Rails i18n指南建议.大多数事情都有效,但是我无法弄清一件事.

as suggested by Rails i18n guide. Most things work, but there's one thing I haven't been able to figure out.

我的所有应用程序路由均已正确映射,例如http://www.example.com/en/catalog/12345(/:locale)/catalog/:id(.:format)正确匹配,并通过{:id=>/[^\/]+(?=\.json|\.html|$|\/)/, :locale=>/en|ja/}路由到catalog#show).所有Devise的URL都可以.一切正常...除了mount -ed Blacklight引擎.

All of my application routes are correctly mapped, e.g. http://www.example.com/en/catalog/12345 correctly matches with (/:locale)/catalog/:id(.:format), and gets routed to catalog#show with {:id=>/[^\/]+(?=\.json|\.html|$|\/)/, :locale=>/en|ja/}). All of Devise's URLs are fine. Everything works... except the mount-ed Blacklight engine.

显然,Blacklight引擎不收听scope. rake routes显示:

Apparently, the Blacklight engine doesn't listen to scope. rake routes shows:

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

而不是我希望的(:locale)/search_history(.:format).

我已经修改了Blacklight模板,以便获得一个用日语和英语同时指向当前页面的语言选择器,但是当我导航到search_history时,遇到:locale参数时url_for突然抛出

I have modified the Blacklight template so that I get a language chooser pointing to the current page in both Japanese and English, but when I navigate to search_history, url_for suddenly throws up when confronted with the :locale parameter.

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

Why does mount ignore scope? How do I solve my problem (engine routes also responding to :locale)?

这是我默认的Blacklight生成的config/routes.rb,用scope进行了修改:

Here's my default Blacklight-generated config/routes.rb, modified with scope:

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

tl; dr: scope为我的所有路由加上前缀,但mount以外的路由除外.为什么以及如何解决?

tl;dr: scope prefixes all of my routes, except the routes by mount. Why, and how to fix?

推荐答案

Rails确实确实忽略了引擎路线中的scope,但是我可以明确地为引擎添加作用域:

It seems that Rails is indeed ignoring scope in the engine routes, but I can add a scope for the engine explicitly:

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

但是,那仍然不能解决我的问题(续 link_to,其中包含引擎路线的参数).

However, that still does not solve my issues (continued in link_to with parameters for an Engine route).

这篇关于`mount`周围的`scope`无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆