Ruby on rails RubyonRails-未初始化常量错误

Ruby on rails RubyonRails-未初始化常量错误,ruby-on-rails,ruby,constants,Ruby On Rails,Ruby,Constants,我正在尝试将ruby文件搜索应用程序转换为rails。我正处于非常初级的阶段。应用程序不会在浏览器中显示任何错误消息,但它甚至不会显示任何结果 当我试着调试时,它显示了关于单位化常数的错误 C:/Workspace/DocumentManagement/app/controllers/document_management_controller.rb:1:in `<top (required)>': uninitialized constant ApplicationControll

我正在尝试将ruby文件搜索应用程序转换为rails。我正处于非常初级的阶段。应用程序不会在浏览器中显示任何错误消息,但它甚至不会显示任何结果

当我试着调试时,它显示了关于单位化常数的错误

C:/Workspace/DocumentManagement/app/controllers/document_management_controller.rb:1:in `<top (required)>': uninitialized constant ApplicationController (NameError)
    from c:/Ruby200/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.22/lib/ruby-debug-ide.rb:86:in `debug_load'
    from c:/Ruby200/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.22/lib/ruby-debug-ide.rb:86:in `debug_program'
    from c:/Ruby200/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.22/bin/rdebug-ide:110:in `<top (required)>'
    from c:/Ruby200/bin/rdebug-ide:23:in `load'
    from c:/Ruby200/bin/rdebug-ide:23:in `<main>'

在app/controllers下,您应该有一个如下所示的application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end

class ApplicationController

它是在您创建新项目“rails new myproject”时自动创建的。

我认为您缺少ApplicationController。所有其他控制器都从该类继承

一个简单的应用程序&u controller.rb

class ApplicationController < ActionController::Base
end

class ApplicationController
当然,您有一个application\u controller.rb文件,对吗?是否定义了application controller?它在app/controllers/application_controller.rbI中自动生成,在controllers文件夹中有application_controller.rb,该文件夹是自动生成的。class ApplicationControllerclass DocumentManagementController < ApplicationController before_filter :doIt # does this on startup def doIt @searchterm=Search.new Content.container end def search @search_p = params[:path] @search_t = params[:term] begin #@searchterm=Search.new(@search_p, @search_t) @searchterm.init @searchterm.distinguish #@searchterm.navigate rescue end end def insertIntoDb end end
class Search
# constructor
def init(a,b)
  @path = Dir.chdir(a)
  @pathaddress = a
  @content=Dir.entries(".")
  @term=b
  @files=Array.new
  @folder=Array.new
  @wordFiles = Array.new
  @pdfFiles = Array.new
  @textFiles = Array.new 
end


def distinguish()
  puts "*************************************************"
  puts "Following files Found"
  begin
      Find.find(@pathaddress) do |path|
        @files << path
        puts path
      end
      puts "*************************************************"
      classifyFile()
     rescue => e
       puts "An exception raised in distinguish Method"
     end
end

# Method to distinguish between file and directory
#def distinguish(dir)
  #begin
   # dir.each do|x|
      #if File.directory?(x) then
       # @folder << x
      #else
       # @files << x
     # end 
   # end
    #classifyFile()
   # rescue => e
    #  puts "An exception raised in distinguish Method"
    #end
#end

# Method to Classify text files, word files and pdf documents
def classifyFile()

  begin
  @files.each do |fileName|
      if fileName.include?(".doc" || ".docx")
      @wordFiles << fileName
    elsif fileName.include?(".pdf")  
      @pdfFiles << fileName
    elsif fileName.include?(".txt") 
      @textFiles  << fileName
    else

     end 
  end 
  processTextFiles()
  processWordFiles()
  processPdfFiles() 
   rescue => e
       puts "An exception raised in classifyFile Method"
    end
end

# Method to check if there any text files to process
def processTextFiles()
  if @textFiles.empty? == false
    searchTextFile()
  end
end

# Method to check if there any Word files to process
def processWordFiles()
  if @wordFiles.empty? == false
    searchWordFile()
  end
end

# Method to check if there any Pdf files to process
def processPdfFiles()
   if @pdfFiles.empty? ==false
      searchPdfFile()
    end
end

# Method to search for Word File
def searchWordFile()
  begin
  require 'win32ole'
  @wordFiles.each do |fileName|
    break if fileName.include?("~")
    #fileName = @pathaddress + "/" + fileName 
    fileName = fileName.gsub("/", "//")
    doc  = WIN32OLE.connect(fileName)
    doc.sentences.each do |x|
     if x.text.include?(@term)
       puts "Found term in " + fileName
       puts "Reading the file contents of " + fileName
       puts "-------------------------------------------------------------------"
       doc.sentences.each do |y|
         Content.add(fileName, y.text)
         puts y.text
       end
       puts "-------------------------------------------------------------------"
      doc.close()
       end
      end
      end
      rescue => e
        puts "An exception raised in searchWordFile Method"
      end
  end


# Method to search PDF files  
def searchPdfFile()
 begin
 @pdfFiles.each do |fileName|
 reader = PDF::Reader.new(fileName)
   reader.pages.each do |page|
        if page.text.include?(@term)
           puts "Found term in " + fileName
           puts "Reading the file contents of " + fileName
           puts "-------------------------------------------------------------------"
           print page.text
           Content.add(fileName, page.text)
        end
       puts "\n-------------------------------------------------------------------"
      end
    end
  rescue => e
     puts "An exception raised in searchPdfFile Method"
  end
end

# Method to search term in files
def searchTextFile() 
begin
  @textFiles.each do |fileName| #iterate through each files
      fileName = fileName.gsub("\\", "//")
     inputArray = File.readlines(fileName) 
     inputArray.each do|line|
       if line.include?(@term)
                puts "Found term in " + fileName
                puts "Reading the file contents of " + fileName
                 puts "-------------------------------------------------------------------"
                printFileContents(fileName)
        end
     end            
  end
  rescue => e
     puts "An exception raised in searchTextFile Method"
  end
end



# Method to print results on screen
def printFileContents(file)
begin
    puts File.read(file)
    puts "-------------------------------------------------------------------"
    rescue => e
     puts "An exception raised in printFileContents Method"
  end
end

end # End of Search class






class Content

def self.container
  @@theContent=Hash.new
end

  def self.add(key,value)
    @@theContent[key]=value
end
  def self.showContent
    @@theContent
  end

end
Rails.application.routes.draw do
  match '/search', to:'document_management#search', via:'get'

  get 'document_management/search'

  get 'document_management/insertIntoDb'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end
class ApplicationController < ActionController::Base
end