2014-05-23 53 views
0

我想将ruby文件搜索应用程序转换为rails。我处于初级阶段。应用程序不会在浏览器中显示任何错误消息,但它甚至不显示任何结果。Ruby on 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>' 

这些应用程序文件: 在查看文件夹:

_form.html.erb

  <%= form_tag("/search", method: "get") do %> #calls controller search method 
     <%= label_tag(:path, "Search for filename including root path:") %> 
     <p> 
     <%= text_field_tag(:path) %> 
     <p></p> 
     <p></p> 
     <%= label_tag(:term, "enter search term:") %> 
     <p> 
     <%= text_field_tag(:term) %> 

    <p></p> 
    <p></p> 
    </p> 
    <p></p> 
    <%= submit_tag("Search") %> 
    <% end %> 

search.html.erb

<% provide(:title, 'Search') %> 
<p> 
This is Page will prompt for the search term and the root directory 
<%= render 'form' %> 
<p></p><p></p><p></p><p></p><p></p><p></p> 

<p>Showing Results</p> 


<% @content=Content.showContent %> 

<% @content=Content.showContent %> 
<% @[email protected] %> 
<% @keys.each do |key| %> 
<h1><strong> <big> <%= key %> </big></strong></h1> 
<p></p><p></p><p></p><p></p><p></p><p></p> 
<% @[email protected][key] %> 
<%@[email protected]_s%> 
<textarea style="width: 600px; height: 350px;"> <%[email protected]%></textarea> 
<%end%> 
<% @content.clear%> 

DocuentManagementController

class 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 

Search.rb在模型文件夹中,包含搜索&内容类

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 
+4

继承可以肯定的,你有一个application_controller.rb文件吗? – jvanbaarsen

+0

是否定义了应用程序控制器?它在app/controllers/application_controller.rb中自动生成 –

回答

0

在应用/控制器,你应该有一个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 

当您创建它自动创建新项目“rails new myproject”

+0

我在控制器文件夹中有自动生成的application_controller.rb。 – user3668443

+0

class ApplicationController user3668443

0

我想你是缺少ApplicationController。所有其他控制器从这个类

简单application_controller.rb

class ApplicationController < ActionController::Base 
end 
相关问题