小编Die*_*sco的帖子

Rails使用活动存储读取csv文件数据

我有这个课程,我正在使用主动存储

class MaterialsUpload < ApplicationRecord
  has_one_attached :csv_file
end
Run Code Online (Sandbox Code Playgroud)

这是附件

#<ActiveStorage::Attached::One:0x007ff1f0be9e90
 @dependent=:purge_later,
 @name="csv_file",
 @record=
  #<MaterialsUpload:0x007ff1f0c604f0
   id: 3,
   success: 0,
   errors_list: [],
   total: 0,
   created_at: Mon, 12 Feb 2018 14:43:35 UTC +00:00,
   updated_at: Mon, 12 Feb 2018 14:43:35 UTC +00:00>>
Run Code Online (Sandbox Code Playgroud)

有没有办法可以读取数据,所以我可以做这样的事情

string = materials_upload.csv_file.read
CSV.parse(csv_string, headers: true) do |row|
    # do something
end
Run Code Online (Sandbox Code Playgroud)

csv ruby-on-rails rails-activestorage

9
推荐指数
1
解决办法
3074
查看次数

标签 统计

csv ×1

rails-activestorage ×1

ruby-on-rails ×1