how to reload routes /config/routes/* in rails 4?

拟墨画扇 提交于 2019-12-20 19:57:29

问题


How to force rails 4 to reload all route files?
That is without having to restart the application to make the routes from /config/routes/.rb get loaded* I did a split them up in /config/routes/.rb but these /config/routes/ files are not reloaded.

This worked in rails 3 but not 4:

#config.paths['config/routes'].unshift *Dir["config/routes/*.rb"]

回答1:


You can use:

Rails.application.reload_routes!

You can read about it here (will have to use find)




回答2:


config.paths["config/routes.rb"] << YOUR_ROUTE_FILE



回答3:


In Rails 3, if you are splitting the routes.rb file into multiple files, we have to add this line to application.rb:

config.paths['config/routes'].concat Dir[Rails.root.join("config/routes/*.rb")]

...and the corresponding routes in config/routes/*.rb files like this:

TestApp::Application.routes.draw do
  namespace :api do
    resources :test
  end
end

In Rails 4, Rails no longer provides a ["config/routes"] key in Rails::Engine.paths. However, in Rails 4, there is no need to add to config.path in application.rb.

Just add the corresponding routes under config/routes/*.rb.



来源:https://stackoverflow.com/questions/18902878/how-to-reload-routes-config-routes-in-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!