如何在 Rails 路由中避免代码重复?
How can code duplication be avoided in Rails routes?
我在 routes.rb
中有很多资源,其中使用和重复的资源很少。
有没有办法避免代码重复?
resources :pages do
resources :comments
##other routes
member { post :vote }
end
resources :videos do
resources :comments
##other routes
member { post :vote }
end
resources :images do
resources :comments
##other routes
member { post :vote }
end
您可以组织您的路线文件,以防止它变成一个巨大的、难以管理的混乱局面。但是,这不是通过关注...它实际上比 "basic" 更多。只需分解文件并将它们包含在主路由文件中:)。
前段时间我读了一篇很好的 post:splitting routes,rb into smaller parts
希望对您有所帮助。
我自己为此使用了 procs。然后我就在我需要它的每个命名空间或资源块中调用它。
但问题可能是更好、更易读的解决方案。
我在 routes.rb
中有很多资源,其中使用和重复的资源很少。
有没有办法避免代码重复?
resources :pages do
resources :comments
##other routes
member { post :vote }
end
resources :videos do
resources :comments
##other routes
member { post :vote }
end
resources :images do
resources :comments
##other routes
member { post :vote }
end
您可以组织您的路线文件,以防止它变成一个巨大的、难以管理的混乱局面。但是,这不是通过关注...它实际上比 "basic" 更多。只需分解文件并将它们包含在主路由文件中:)。
前段时间我读了一篇很好的 post:splitting routes,rb into smaller parts
希望对您有所帮助。
我自己为此使用了 procs。然后我就在我需要它的每个命名空间或资源块中调用它。
但问题可能是更好、更易读的解决方案。