No Route Prefix Named Rails Engine

I am converting an application to use Rails engines. I have an engine located in a folder engines/web. In config/routes.rbI mount it like this:

mount Web::Engine => '/', as: 'web_engine'

The folder structure is as follows:

config
  routes.rb
engines
  web
    config
      routes.rb
    app
      controllers
        web 
          application_controller.rb
          ...
    lib
      ...

The engine is defined as follows:

module Web
  class Engine < Rails::Engine
    isolate_namespace Web
  end
end

My application controller inside the web engine is defined as follows:

module Web
  class ApplicationController < ::ActionController::Base
    layout 'web/layouts/application'

    # other code
  end
end

The problem is that inside Web::ApplicationControllerI have to refer to the routes as web_engine.my_route_path, not how my_route_path. Is there a way to access routes without a prefix web_enginefrom within the web engine?

+4
source share
2 answers

This should do the trick:

module Web
  class Engine < Rails::Engine
    isolate_namespace Web
  end
end

. docs. Engine.isolate_namespace.

0

1- isolated_namespace _engine_path_/lib/web/engine.rb( , )

2- mount Web::Engine ... _app_path_/config/route.rb

3- Rails.application.routes.draw. , _engine_path_/config/routes.rb :

Rails.application.routes.draw do
  resources :things
  get 'my_path', to: 'my_controller#my_action'
end

, ,

,

-1

Source: https://habr.com/ru/post/1548003/


All Articles