Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 正在检查上载的文件是否为csv_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby 正在检查上载的文件是否为csv

Ruby 正在检查上载的文件是否为csv,ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,我有一个叫做导入文件的方法,我正试图上传一个CSV。我想检查上传的文件是否为csv格式。我确实做了那件事 但这不起作用,所以我尝试在控制器中编写这个开始和救援块 当上传其他格式的文件时,无法挽救错误。我得到这个错误“ SheetEntriesController中的参数错误#导入 “当我上传.jpg或.xls文件时,有人能帮我吗 require "csv" class SheetEntriesInvalid < Exception end class SheetEntriesCont

我有一个叫做导入文件的方法,我正试图上传一个CSV。我想检查上传的文件是否为csv格式。我确实做了那件事 但这不起作用,所以我尝试在控制器中编写这个开始和救援块 当上传其他格式的文件时,无法挽救错误。我得到这个错误“ SheetEntriesController中的参数错误#导入 “当我上传.jpg或.xls文件时,有人能帮我吗

require "csv"
  class SheetEntriesInvalid < Exception
end

class SheetEntriesController < EntryController

  unloadable

  def import
    if (params[:upload_entries]).present?
      begin
        SheetEntry.import(params[:upload_entries].path)
        redirect_to root_url, notice: "Updated Succesfully"
      rescue 
        redirect_to root_url, notice: "Invalid CSV file format."
      end
    end
  else
    redirect_to root_url, error: "No File Chosen"
  end 
  end
end
需要“csv”
类SheetEntriesInvalid<异常
结束
类SheetEntriesController<入口控制器
不值得
def导入
如果(参数[:上传_条目])存在?
开始
SheetEntry.import(参数[:上载条目].path)
重定向到根目录url,注意:“已成功更新”
拯救
重定向到根url,注意:“无效的CSV文件格式。”
结束
结束
其他的
将\u重定向到根\u url,错误:“未选择文件”
结束
结束
结束

一种方法是处理文件上传。通常,使用异常来控制流被认为是不好的做法。使用曲别针,您可以在模型中预先对文件的内容类型进行验证,而不是依靠
SheetEntry.import()
引发异常

class SheetEntry < ActiveRecord::Base
  has_attached_filed :csv
  validates_attachment :csv, content_type: { content_type: ['text/csv','text/plain']} # 'csv' files may have text/plain as a content-type.
  # ...
end
class SheetEntry
一种方法是处理文件上传。通常,使用异常来控制流被认为是不好的做法。使用曲别针,您可以在模型中预先对文件的内容类型进行验证,而不是依靠
SheetEntry.import()
引发异常

class SheetEntry < ActiveRecord::Base
  has_attached_filed :csv
  validates_attachment :csv, content_type: { content_type: ['text/csv','text/plain']} # 'csv' files may have text/plain as a content-type.
  # ...
end
class SheetEntry
您可以检查文件类型

class SheetEntriesController < EntryController
  def create
    puts MIME::Types.type_for(file_path).first.content_type # "text/csv"
    if MIME::Types.type_for(file_path).first.content_type == 'text/csv'
      ...
    end
  end
end
class SheetEntriesController

我希望这对您有所帮助

您可以检查文件类型

class SheetEntriesController < EntryController
  def create
    puts MIME::Types.type_for(file_path).first.content_type # "text/csv"
    if MIME::Types.type_for(file_path).first.content_type == 'text/csv'
      ...
    end
  end
end
class SheetEntriesController

我希望这有帮助

您的代码甚至无法正确解析。您的代码甚至无法正确解析。谢谢!我还想知道如何使用begin和rescue语句来实现它。我认为这种方法比试图在控制器中拯救异常更彻底
rescue
在没有特定异常的情况下捕获所有
StandardError
s,而不一定只是您想要捕获的。在任何情况下,为了知道如何正确捕获异常,我们需要知道如何定义
SheetEntry.import
。如果您有兴趣知道为什么使用异常控制流是一种不好的做法,这里还有一个很长的讨论:或者谢谢!我还想知道如何使用begin和rescue语句来实现它。我认为这种方法比试图在控制器中拯救异常更彻底
rescue
在没有特定异常的情况下捕获所有
StandardError
s,而不一定只是您想要捕获的。在任何情况下,为了知道如何正确捕获异常,我们需要知道如何定义
SheetEntry.import
,如果您有兴趣知道为什么使用异常控制流是一种不好的做法,这里还有一个很长的讨论:或者