Loading routes from the lib folder in full rail mode

I made a full engine and converted some plugins to work with the engine (I put them in lib /) and loaded them into the engine.rb initializer

This is the structure:

  • Appendix
    • configuration
      • routes.rb
  • Lib
    • plugin
      • configuration
        • routes.rb

In the routes in app / config / routes.rb I have:

Rails.application.routes.draw do match 'help', :to => 'help#index', :as => 'help' match 'login', :to => 'sessions#new', :as => 'login' match 'logout', :to => 'sessions#destroy', :as => 'logout' match 'loadtest', :to => 'loadtests#index', :as => 'loadtest' end 

In the second route.rb file in (lib / plugin / config / routes.rb) I have this:

 Rails.application.routes.draw do match '/mailchimp/callback', :to => 'mailchimp#callback', :as => 'mailchimp_unsubscribe' end 

In my engine.rb in config / initializers / I put:

 require "#{File.dirname(__FILE__)}/../../lib/plugin/config/routes" 

Now when I run rake app: routes, I get this as output:

  help /help(.:format) help#index login /login(.:format) sessions#new logout /logout(.:format) sessions#destroy loadtest /loadtest(.:format) loadtests#index 

How can I add routes from the plugin to engine routes?

+4
source share
2 answers

Modify config / application.rb and add this line:

 config.paths["config/routes"] << Rails.root.join('lib/plugin/config/routes.rb') 

It should work.

+1
source

enter the code in application.rb

load all * .rb into lib / routes

 for rout in Dir[Rails.root.join('lib','routes', '*.{rb,yml}').to_s] config.paths["config/routes"] << rout end 
0
source

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


All Articles