Skip to content

Configuring rails routes

Chris Beer edited this page Feb 5, 2021 · 5 revisions

Customizing the path segments from routes

Blacklight generates most of the routes it uses through routing concerns. This means that applications can easily use any of Rails' routing features to configure and customize their routes. For example, this out-of-the-box configuring exposes the CatalogController at /catalog

  resource :catalog, only: [:index], as: 'catalog', path: '/catalog', controller: 'catalog' do
    concerns :searchable
  end

but this can be easily customized to use e.g. /search instead:

  resource :catalog, only: [:index], as: 'catalog', path: '/search', controller: 'catalog' do
    concerns :searchable
  end

Similarly, the show page for individual documents can be changed to /view/:id:

resources :solr_documents, only: [:show], path: '/view', controller: 'catalog' do
  concerns :exportable
end

Working with Solr IDs with special characters

If your solr ids happen to have dots in them, Rails is going interpret the dots as the separator between the file name and the file exension, and your documents won't be displayed on the show view. In order to fix that you can override the default routes like this:

MyApp::Application.routes.draw do
  ...
  blacklight_for :catalog, constraints: { id: /[a-zA-Z0-9_.:]+/, format: false }
  ...
end

For newer versions of Blacklight where blacklight_for has been deprecated, you can add constraints to the resource route for solr_documents like so:

resources :solr_documents, only: [:show], path: '/catalog', controller: 'catalog', constraints: { id: /[a-zA-Z0-9_.:]+/ } do
  concerns :exportable
end

See: http://guides.rubyonrails.org/routing.html#specifying-constraints for more details on rails routing and routing constraints.

Clone this wiki locally