我编写了以下代码并且它可以工作,但是有没有更好的方法来包含文件而不是评估文件的内容?我试图只调用 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