我编写了以下代码并且它有效,但有没有更好的方法来包含文件而不是评估文件的内容?我试图只调用require,然后在mainObj的上下文中处理文件。
想法?
module ActionDispatch
module Routing
class Mapper
def include_routes(file)
file = File.new("#{Rails.root}/config/routes/#{file}.rb", "r")
buffer = ""
while (line = file.gets)
buffer += line
end
file.close
eval(buffer)
end
end
end
end
单个文件看起来像这样
resources :brands do
collection do
post :sort
get :published
get :unpublished
get :deleted
end
member do
get :notes
get :undelete
get :delete
post :move_node
get :get_node_children
end
resources :notes, :only => [:new, :create]
end
get "brands/filter/:type" => "brands#filter", :as => 'filter_brands'
路线文件看起来像这样
KohcmsV4::Application.routes.draw do
devise_for :users
mount_languages
namespace :admin do
root :to => 'admin#index'
include_routes "admin/media"
include_routes "admin/videos"
include_routes "admin/roles"
include_routes "admin/users"
include_routes "admin/images"
include_routes "admin/videos"
language_scope do
include_routes "admin/pages"
include_routes "admin/brands"
include_routes "admin/pcategories"
include_routes "admin/products"
include_routes "admin/procedures"
include_routes "admin/sections"
end
end
root :to => 'application#index'
#match '*not_found', to: 'errors#error_404'
end
答案 0 :(得分:0)
在routes.rb
中,您可以执行以下操作:
Dir["#{Rails.root}/config/routes/*.rb"].each do |route_file|
load route_file
end
如果您使用load
方法,则需要将子路由文件更改为如下所示:
KohcmsV4::Application.routes.draw do
namespace :admin do
resources :brands do
collection do
post :sort
get :published
get :unpublished
get :deleted
end
...
end
end