2014-04-01 71 views
1

也許有更好的方法來做到這一點。我想能夠動態加載一些路由。我打算在routes.rb有靜態路線,custom_routes.rb有自定義路線。然後在routes.rb底部,我會做:僅當文件存在時才需要文件

CustomRoutes.create if defined?(CustomRoutes) 

爲了這個工作,我有權要求custom_routes.rb只有當該文件存在,但如何?

custom_routes.rb

class CustomRoutes 
    def self.create 
    MyApplication.routes.draw do 
     #...more routes here 
    end 
    end 
end 

回答

2

你可以這樣做:

require 'custom_routes' if File.exists?('custom_routes.rb') 

雖然你可能想添加一些路徑信息:

require 'custom_routes' if File.exists?(File.join(Rails.root, 'lib', 'custom_routes.rb')) 
+0

謝謝,這是有效的。 –

0

喜歡的東西

require File.expand_path("../../config/custom_routes", __FILE__) if File..exists?("../../config/custom_routes.rb")

相關問題