[英]Ruby on Rails - Uninitialized Constant Error
我正在嘗試將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>'
這些是應用程序文件:在“查看”文件夾中:
_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 %>
<% @keys=@content.keys %>
<% @keys.each do |key| %>
<h1><strong> <big> <%= key %> </big></strong></h1>
<p></p><p></p><p></p><p></p><p></p><p></p>
<% @value=@content[key] %>
<%@v=@value.to_s%>
<textarea style="width: 600px; height: 350px;"> <%=@v%></textarea>
<%end%>
<% @content.clear%>
文檔管理器
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
在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
當您創建新項目“ rails new myproject”時,它會自動創建
我認為您缺少ApplicationController。 所有其他控制器都繼承自此類
一個簡單的application_controller.rb
class ApplicationController < ActionController::Base
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.