0

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

1 回答 1

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
于 2012-11-28T21:59:06.493 回答