从完整 Rails 引擎中的 lib 文件夹加载路线
我制作了一个完整的引擎并转换了一些插件以与引擎一起工作(我将它们放在 lib/ 中)并将它们加载到初始化程序 engine.rb
这是结构:
- app
- 配置
- 路线.rb
- 配置
- 库
- 插件
- 配置
- 路线.rb
- 配置
- 插件
在 app/config/routes.rb 中的路由中,我有:
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
在( lib/plugin/config/routes.rb )中的第二个routes.rb 文件中,我有:
Rails.application.routes.draw do
match '/mailchimp/callback', :to => 'mailchimp#callback', :as => 'mailchimp_unsubscribe'
end
在我的engine.rb 中config/initializers/ 我放置:
require "#{File.dirname(__FILE__)}/../../lib/plugin/config/routes"
现在,当我运行 rake app:routes 时,我得到以下输出:
help /help(.:format) help#index
login /login(.:format) sessions#new
logout /logout(.:format) sessions#destroy
loadtest /loadtest(.:format) loadtests#index
如何将插件的路由添加到引擎路由?
I made a full engine and converted some plugins to work together with the engine ( I put them in lib/ ) and load them in an initializer engine.rb
This is the structure :
- app
- config
- routes.rb
- config
- lib
- plugin
- config
- routes.rb
- config
- plugin
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 routes.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 the routes from the plugin to the engine routes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑 config/application.rb 并添加此行:
它应该可以工作。
Edit config/application.rb and add this line:
It should works.
将代码放入 application.rb
加载 lib/routes 中的所有 *.rb
put the code in application.rb
load all *.rb in lib/routes