Do not prefix routes with Rails Engine name

rails engine rspec
rails engine routes
rails engine gem
rails routes
rails engine vs gem
rails 6 engines
rails routes mount
rails engine migrations

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

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

The folder structure is like this:

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

Engine is defined like this:

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

My application controller inside of Web engine is defined like this:

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

    # other code
  end
end

The problem is that inside of Web::ApplicationController I have to refer to routes as web_engine.my_route_path and not as my_route_path. Is there a way to access routes without web_engine prefix from inside of Web engine?

This should do the trick:

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

See docs for details about routes. Also check out Engine.isolate_namespace description.

Rails::Engine, Note: Engines and Plugins are not exactly the same in the Ruby on Rails file is: Rails.application.routes.draw ; however, within an Engine, the name of rake routes Prefix Verb URI Pattern passages /passages users GET� Your prefix becomes static, but in the most cases its ok If you have some external or internal engines with own routes, that are automatically mounted inside root Rails application on startup, they will be without prefix. But there exists a solution for that. Map your entire Rails application by some prefix like this:

Include your url helpers inside your engine's controller:

module Web
  class ApplicationController < ::ActionController::Base
    helper Web::Enginer.routes.url_helpers

  end
end

Getting Started with Engines — Ruby on Rails Guides, Steps to reproduce Configure a Rails application designed to be deployed in a See that its URL routing helpers do not contain the proper subdirectory which would be stripped as that logic would assume it was an engine name. allow app prefix wo/ breaking engine url helpers dradis/dradis-ce#323. Supported mountable engine's route prefix (or namespace), which was added in Rails 3.1. Refinery CMS 2, the most popular cms which supports Rails 3.2, requires this feature because it has highly modularized design and it is common to refer to other engine's url from view.

You must

1- remove the line isolated_namespace from you _engine_path_/lib/web/engine.rb file (I assume you know the consequences in terms of risks of object naming colliding)

2- remove the line mount Web::Engine ... from your _app_path_/config/route.rb file

3- define your routes in your engine in a Rails.application.routes.draw block. So your _engine_path_/config/routes.rb look like:

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

Then your routes defined in your engine won't be prefixed by the engine name

And you will have access to your route helpers from your engine in your app and vice-versa, without prefix and without scoping to your engine name

How to Build a Ruby on Rails Engine, The application and its engines can have routes with the same names. Routes defined in Engine's pages will have URL prefix 'myengine/'. Define routes in Engine with Rails.application.routes.draw, NOT Myengine::Engine.routes.draw:. It will also set MyEngine.table_name_prefix to “my_engine_”, meaning for example that MyEngine::Article will use the my_engine_articles database table by default. Using Engine's routes outside Engine. Since you can now mount an engine inside application's routes, you do not have direct access to Engine's url_helpers inside Application. When

Mounted engine at root fails to generate correct URLs when using a , lib", __FILE__) Gem::Specification.new do |s| s.name = "foo_baz" s.version If engine is not namespaced, its routing proxy prefix would be� rails g model user name:string named routes and they will not clash. Routes inside an engine are drawn on without the main_app prefix from within the engine.

Integrating Gem/Engine and Main Rails App � GitHub, Routes for the engine are defined just like a normal rails app, in the routes.rb file. same resources names in both main Rails application and mounted engine. If engine is not namespaced, its routing proxy prefix would be� I actually ran the rails s command and table_name_prefix does not get called. I discovered the issue when trying to load the rails_admin root (dashboard) See sferik/rails_admin#3062. Any suggestions on getting rails to load the module the model is namespaced under first to get the table_name_prefix to get set properly? rails g model collective

Rails mountable engines, I often see beginners not taking full advantage of the powerful routing engine provided by Ruby on Rails. The routing system in Rails is the system that examines the URL of an incoming request and determines routes as defined manually before, but it also conveniently names each route with a prefix. If you specify this option at any level of routing, then paths for nested resources which reference a specific member (that is, those with an :id parameter) will not use the parent path prefix or name prefix. To see what this means, consider this set of routes:

Comments
  • Hi Andrew. Could you please update your question with the code on your engines/web/config/routes.rb, config/routes files and the output of rake/routes?
  • Looking at the docs, I cannot tell if this will force the engine files to be restructured.
  • @NewAlexandria routes section is written on the assumption that you use isolated namespace. It containts these words: For instance, the following example would go to the application's posts_path if that template was rendered from the application, or the engine's posts_path if it was rendered from the engine: link_to "Blog posts", posts_path.
  • This is what I have. Still inside of controllers in Web engine I have to prefix all routes with "web": web.my_route_path
  • @AndrewBezzub it's definitely should work. Here is some very nice description: edgeapi.rubyonrails.org/classes/Rails/Engine.html . I did a test and everything worked well. Also I had to change railsties' loading order (see docs for my link for details for option called railties_order) via config.railties_order = [TestEngine::Engine, :main_app, :all] to make it work with mounting to '/'. Try it.
  • @FlashGordon looking at your link, it says that the engine's paths are called exactly the way that Andrew does not want to call them; as my_engine.root_url
  • is there a way to not prefix routes with mountable engine?
  • I am OK with prefixing routes in the app, I don't want them to be prefixed in code that is part of the engine
  • is there a way to not prefix routes with mountable engine? Well yes that's what my answer is about. Just follow the 3 steps in my answer and your routes won't be prefixed anymore
  • it is not a "mountable" engine
  • Look, I have on project with 5 engine, and my routes are not prefixed. Follow my steps and it will work. Guaranteed