0

(雖然這裏討論了Blacklight引擎,但我相信這個問題實際上完全是關於Rails的。)'mount'附近的`scope'無效?

我想向我的Blacklight應用程序添加國際化。爲此,我

  • 包裹在config/routes.rb一切都變成scope "(:locale)", locale: /en|ja/

  • app/controllers/application_controller.rb我加入before_action :set_locale和推翻default_url_options

通過Rails i18n guide的建議。大多數情況下都可以工作,但有一件事我一直無法解決。

我所有的應用程序路徑都被正確映射,例如, http://www.example.com/en/catalog/12345(/:locale)/catalog/:id(.:format)正確匹配,並通過{:id=>/[^\/]+(?=\.json|\.html|$|\/)/, :locale=>/en|ja/}路由到catalog#show)。 Devise的所有網址都很好。一切正常......除了mount -ed Blacklight引擎。

顯然,Blacklight引擎不聽scoperake routes顯示:

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

,而不是作爲(:locale)/search_history(.:format)我希望。

我已經修改了黑光模板,使我得到一個語言選擇器指向當前頁面的日語和英語,但是當我瀏覽到search_historyurl_for當與:locale參數面臨突然拋出了。

爲什麼mount忽略scope?我如何解決我的問題(引擎路線也迴應:locale)?

這裏是我的默認黑光燈產生的config/routes.rb,與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;博士:scope前綴我的所有航線,除由mount路線。爲什麼,以及如何解決?

回答

0

看來,Rails的確實忽略了發動機線路scope,但我可以爲引擎明確添加範圍:

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

但是,這仍然沒有解決我的問題(在link_to with parameters for an Engine route續)。