Ruby on rails ruby类在rails应用程序中似乎停留在过去,即使在重新启动rails应用程序之后

Ruby on rails ruby类在rails应用程序中似乎停留在过去,即使在重新启动rails应用程序之后,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我已经更改了if/then块,但仍然在异常日志中看到历史错误。似乎rails解释器被困在了过去。。。我重新启动了服务器好几次 while ctr <= 11 do @filesample << row.to_hash ctr += 1 if ctr > 11 then return @filesample else return @filesample end end C:/Sites/chartstoke/app/src/file\

我已经更改了if/then块,但仍然在异常日志中看到历史错误。似乎rails解释器被困在了过去。。。我重新启动了服务器好几次

while ctr <= 11 do
  @filesample << row.to_hash
  ctr += 1
  if ctr > 11 then
    return @filesample
  else
    return @filesample
  end
end
C:/Sites/chartstoke/app/src/file\u processor\u service.rb:15:语法 错误,意外“'),应为关键字_then或“;”或“\n”(如果ctr> 11) do^C:/Sites/chartstoke/app/src/file\u processor\u service.rb:17: 语法错误,意外的关键字_else,应为“')”else^ C:/Sites/chartstoke/app/src/file\u processor\u service.rb:19:syntax 错误,意外关键字_end,应为“')”end^ C:/Sites/chartstoke/app/src/file\u processor\u service.rb:63:语法 错误,意外关键字_end,应为“')”end^

while ctr <= 11 do
  @filesample << row.to_hash
  ctr += 1
  if ctr > 11 then
    return @filesample
  else
    return @filesample
  end
end
在我的application.rb文件中,我已自动加载:

config.autoload_paths += %w(#{config.root}/services #{config.root}/src)
while ctr <= 11 do
  @filesample << row.to_hash
  ctr += 1
  if ctr > 11 then
    return @filesample
  else
    return @filesample
  end
end
这是我的控制器:

class FileProcessorController < ApplicationController

    def index
    end

    def upload
    end

    def import
        file_processor = FileProcessorService.new
        @file_sample = file_processor.present_data_sample(params[:file].path)
        if (@file_sample) then
            render 'index'
        end
    end
end
while ctr <= 11 do
  @filesample << row.to_hash
  ctr += 1
  if ctr > 11 then
    return @filesample
  else
    return @filesample
  end
end
class FileProcessorController
这是我的Ruby文件\u processor\u service.rb类:

class FileProcessorService

    require 'csv'
    require 'elasticsearch'

    def initialize
    end

    # This method will just parse the first 10 - 15 rows for user to see data sample.
    def present_data_sample(file_path)
        @filesample = {}
        ctr=0
        uploaded_file_path = file_path
        CSV.foreach(uploaded_file_path, headers: true) do |row|
            while ctr <= 11 do
                @filesample << row.to_hash
                ctr += 1
                if ctr > 11 then
                    return @filesample
                else
                    return @filesample
                end
            end
        end
    end


    def load_index(file)
        uploaded_file_path = params[:file].path
        index_name = File.basename(uploaded_file_path, ".*")
        ctr=1
        CSV.foreach(uploaded_file_path, headers: true) do |row|
            @es_client.index(index: index_name,
                type:  index_name+"_type",
                id: index_name+"."+ctr,
                body: row.to_hash
                )
            ctr +=1
        end
    end


        # def build_indexing_body(key, value)

        # end


        # private 

        # def establish_client_node(server)
        #   @es_client = Elasticsearch::Client.new(hosts: ['#{server}:9200'], reload_connections: true)

        # end
    end
end
while ctr <= 11 do
  @filesample << row.to_hash
  ctr += 1
  if ctr > 11 then
    return @filesample
  else
    return @filesample
  end
end
类文件处理器服务
需要“csv”
需要“弹性搜索”
def初始化
结束
#此方法将只解析前10-15行,以便用户查看数据样本。
def显示数据样本(文件路径)
@filesample={}
ctr=0
上传的文件路径=文件路径
CSV.foreach(上传的文件路径,标题:true)do |行|
而ctr

while ctr <= 11 do
  @filesample << row.to_hash
  ctr += 1
  if ctr > 11 then
    return @filesample
  else
    return @filesample
  end
end

而ctr您当前的数据样本方法需要重写。您不应该在块内返回,因为您将退出块。这可能会修复错误,具体取决于代码的其余部分:

while ctr <= 11 do
  @filesample << row.to_hash
  ctr += 1
  if ctr > 11 then
    return @filesample
  else
    return @filesample
  end
end
def present_data_sample(file_path)
    @filesample = {}
    ctr=0
    uploaded_file_path = file_path
    CSV.foreach(uploaded_file_path, headers: true) do |row|
       ctr += 1
       break if ctr > 11
       @filesample << row.to_hash   
    end

    return @filesample
end
def显示数据样本(文件路径)
@filesample={}
ctr=0
上传的文件路径=文件路径
CSV.foreach(上传的文件路径,标题:true)do |行|
ctr+=1
如果ctr>11,则中断

@文件示例您所说的
是什么意思我在异常日志中看到一个历史错误
?你真的在运行应用程序时遇到这个错误吗?我以为我的if缺少了一个
end
关键字和一个
then
。一切都解决了。但我仍然看到同样的错误。我不认为我的语法有什么问题,但是。。。为什么会出现上述错误?您确定要粘贴更新的文件吗?是否可以在结尾处不使用附加的
end
重试?在“ctr>11 then”之后有相同的返回值,因此没有不必要的代码。另外,您永远不会重置ctr,因此在第一行之后将永远不会得到@filesample的结果。不相关,但present_data_sample不会像文档中那样工作-return语句将从函数第一次返回到,10次循环后不返回请解释原因?
那么ruby中的
if
块不需要
,这与不需要显式
do
相同,并且您只想在循环10次后返回,我想这样就不需要else了。不管重构如何。我在FileProcessorService中放置了hello world方法。即使这样也会产生同样的准确误差。问题似乎出在控制器中FileProcessorService类的实例化中
file\u processor=FileProcessorService.new
。。Rails抛出了同样的错误!除了helloworld方法,我删掉了那个类中的所有内容。。。还是没有雪茄!!好吧不管重构如何。我在FileProcessorService中放置了hello world方法。即使这样也会产生同样的准确误差。问题似乎出在控制器中FileProcessorService类的实例化中
file\u processor=FileProcessorService.new
。。Rails抛出了同样的错误!除了helloworld方法,我删掉了那个类中的所有内容。。。还是没有雪茄!!我明白了。真是愚蠢的设置在我这边。谢谢