Ruby on rails 在rails应用程序中保存并稍后导入xls、csv文件

Ruby on rails 在rails应用程序中保存并稍后导入xls、csv文件,ruby-on-rails,Ruby On Rails,在我的ruby on rails应用程序中,我实现了一个查看railscast视频的工具,它将同时直接导入和更新db,但是当文件太大时,它会产生问题 因此,我想编写一个工具,在我的应用程序中上载csv或excel文件,并将其保存在一个目录中。然后我想添加一些观察者来观察目录的内容,在类似于创建或更新目录中的文件的事件中,这些文件的内容将被上传到db中。我不知道该怎么做。 提前感谢。我认为最好的方法是使用与请求分开的worker导入和转换 假设您有一个控制器来添加Excel文件,我将其称为Info

在我的ruby on rails应用程序中,我实现了一个查看railscast视频的工具,它将同时直接导入和更新db,但是当文件太大时,它会产生问题

因此,我想编写一个工具,在我的应用程序中上载csv或excel文件,并将其保存在一个目录中。然后我想添加一些观察者来观察目录的内容,在类似于创建或更新目录中的文件的事件中,这些文件的内容将被上传到db中。我不知道该怎么做。
提前感谢。

我认为最好的方法是使用与请求分开的worker导入和转换

假设您有一个控制器来添加Excel文件,我将其称为
Information
model:

class InformationController < ApplicationController
  def create
    @information = Information.new(params[:information])
    if @information.save
      resque = Resque.enqueue(ImportDataJob, @information.id)
      redirect_to @information, :notice => "Successfully created information for further processing."
    else
      render :new
    end
  end
end
您将在中找到完整的教程,其中介绍了如何将Resque添加到现有Rails应用程序中


注意:自述文件与Resque的实际实现之间存在冲突。显然,他们想改变调用Resque的方式(在自述文件中),但尚未实现。请参阅此了解更多详细信息。

作为Resque的替代方案(我确实认为在后台工作中执行此操作是最好的方法),另请参阅生成,以前称为生成:

超低维护。在控制器操作中,执行以下操作

@file = <uploaded file here>
spawn do 
  #perform some long-running process using @file, in a new process
end
#current thread carries on straight away.
@文件=
产卵
#在新进程中使用@file执行一些长时间运行的进程
结束
#当前线程直接进行。
如果您需要测试它是否已完成(或就此崩溃),您可以保存新进程的id,如下所示:

@file = <uploaded file here>
spawner = spawn do 
  #perform some long-running process using @file, in a new process
end

#current thread carries on straight away.

spawner = spawn do
  #make it in a temp file so serve_if_present doesn't serve a halfmade file
  FileUtils.rm @filename if File.exists?(@filename)
  temp_filename = "#{@filename}.temp"
  ldb "temp_filename = #{temp_filename}"
  current_user.music_service.build_all_schools_and_users_xls(:filename => temp_filename)
  FileUtils.mv temp_filename, @filename
end
@spawn_id = spawner.handle
@文件=
产卵器=产卵器
#在新进程中使用@file执行一些长时间运行的进程
结束
#当前线程直接进行。
产卵器=产卵器
#将其放入临时文件中,以便在当前文件未送达半成品文件时送达
FileUtils.rm@filename如果File.exists?(@filename)
temp_filename=“#{@filename}.temp”
ldb“临时文件名={临时文件名}”
当前用户。音乐服务。构建所有学校和用户(:filename=>temp\u filename)
FileUtils.mv temp_filename,@filename
结束
@spawn_id=spawner.handle

在回答关于Heroku()的评论(似乎已经消失了)-似乎Resque将在Heroku()上工作,我不确定spawn的情况。如果Heroku是一个因素,那么我认为你应该修改你的问题,说这是针对Heroku托管的应用程序。Max Williams-是我,但我不确定我的问题是否相关。非常感谢!Joy我没有看到你对答案的任何评论。。。你要找的就是这些吗?不,实际上我不想用Resque。我一直在寻找一些东西,以便csv数据导入事件,如更新和创建一个文件,在某个目录下的文件正在上载。为什么不重新设置(或任何工人宝石)?您正在寻找的解决方案必须涉及对文件系统的某种轮询,这是一个坏主意,除非您没有更好的替代方案。在任何情况下,如果你想得到你所需要的答案,你能在问题的更新部分补充你不需要任何类型的工人吗?
@file = <uploaded file here>
spawner = spawn do 
  #perform some long-running process using @file, in a new process
end

#current thread carries on straight away.

spawner = spawn do
  #make it in a temp file so serve_if_present doesn't serve a halfmade file
  FileUtils.rm @filename if File.exists?(@filename)
  temp_filename = "#{@filename}.temp"
  ldb "temp_filename = #{temp_filename}"
  current_user.music_service.build_all_schools_and_users_xls(:filename => temp_filename)
  FileUtils.mv temp_filename, @filename
end
@spawn_id = spawner.handle