从完整 Rails 引擎中的 lib 文件夹加载路线

发布于 2025-01-05 20:01:52 字数 1238 浏览 3 评论 0原文

我制作了一个完整的引擎并转换了一些插件以与引擎一起工作(我将它们放在 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
  • lib
    • plugin
      • config
        • 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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再让梦枯萎 2025-01-12 20:01:52

编辑 config/application.rb 并添加此行:

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

它应该可以工作。

Edit config/application.rb and add this line:

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

It should works.

温折酒 2025-01-12 20:01:52

将代码放入 application.rb

加载 lib/routes 中的所有 *.rb

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

put the code in application.rb

load all *.rb in lib/routes

for rout in Dir[Rails.root.join('lib','routes', '*.{rb,yml}').to_s]
config.paths["config/routes"] << rout
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文