在 rails 4 中重构一个大的 routes.rb 文件 [英] Refactoring a large routes.rb file in rails 4
问题描述
我正在将 rails 3 应用程序升级到 rails 4.0.1.
I'm in the process of upgrading a rails 3 app to rails 4.0.1.
在我的 rails 3 应用程序中,我的 application.rb
中有以下代码来使用多个路由文件.
In my rails 3 app I have the following code in the my application.rb
to use multiple route files.
config.paths["config/routes"] += Dir[Rails.root.join('config','路线', '*.rb').to_s]
config.paths["config/routes"] += Dir[Rails.root.join('config', 'routes', '*.rb').to_s]
但是当我尝试在 rails 4 中使用相同的东西时会引发异常.
but that throws an exception when I try to use the same thing in rails 4.
有什么建议吗?
推荐答案
在我的一个较大的应用程序中,我在我的 config/routes.rb 文件中使用了以下代码段.
In one of my larger applications I use the following segment of code inside of my config/routes.rb file.
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
YourApplication::Application.routes.draw do
# Loads config/routes/common.rb
draw :common
# Loads config/routes/another.rb
draw :another
end
<小时>
Rails 4 最初支持 draw :routeName
,但它被删除,因为它没有显示任何改进.(我不知道 ^.^) 你可以在这里查看 git commit:https://github.com/rails/rails/commit/5e7d6bba79393de0279917f93b82f3b7b176f4b5
Rails 4 initially had support for draw :routeName
but it was removed as it did not show any improvements. (I dont know ^.^) You can check out the git commit doing so here: https://github.com/rails/rails/commit/5e7d6bba79393de0279917f93b82f3b7b176f4b5
这篇关于在 rails 4 中重构一个大的 routes.rb 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!