如何使用Paperclip上传CSV

时间:2013-09-24 21:42:50

标签: ruby-on-rails ruby-on-rails-3 csv paperclip

我查看过有关使用Paperclip创建CSV的其他帖子,但我仍然有点迷失为什么这不起作用。我有一个生成CSV字符串的方法(使用CSV.generate),我尝试使用以下方法将其保存到Reports Controller中的Report:

  def create(type)
    case type
    when "Geo"
      csv_string = Report.generate_geo_report
    end

    @report = Report.new(type: type)
    @report.csv_file = StringIO.new(csv_string)

    if @report.save
      puts @report.csv_file_file_name
    else
      @report.errors.full_messages.to_sentence
    end
  end

然而,执行时,我收到undefined method 'stringify_keys' for "Geo":String错误。以下是报告模型:

class Report < ActiveRecord::Base
  attr_accessible :csv_file, :type
  has_attached_file :csv_file, PAPERCLIP_OPTIONS.merge(
    :default_url => "//s3.amazonaws.com/production-recruittalk/media/avatar-placeholder.gif",
    :styles => {
      :"259x259" => "259x259^"
    },
    :convert_options => {
      :"259x259" => "-background transparent -auto-orient -gravity center -extent 259x259"
    }
  )

  def self.generate_geo_report
    male_count = 0
    female_count = 0
    csv_string = CSV.generate do |csv|
      csv << ["First Name", "Last Name", "Email", "Gender", "City", "State", "School", "Created At", "Updated At"]
      Athlete.all.sort_by{ |a| a.id }.each do |athlete|
        first_name = athlete.first_name || ""
        last_name = athlete.last_name || ""
        email = athlete.email || ""
        if !athlete.sports.blank?
          if athlete.sports.first.name.split(" ", 2).first.include?("Women's")
            gender = "Female"
            female_count += 1
          else
            gender = "Male"
            male_count += 1
          end
        else
          gender = ""
        end
        city = athlete.city_id? ? athlete.city.name : ""
        state = athlete.state || ""
        school = athlete.school_id? ? athlete.school.name : ""
        created_at = "#{athlete.created_at.to_date.to_s[0..10].gsub(" ", "0")} #{athlete.created_at.to_s.strip}"
        updated_at = "#{athlete.updated_at.to_date.to_s[0..10].gsub(" ", "0")} #{athlete.updated_at.to_s.strip}"
        csv << [first_name, last_name, email, gender, city, state, school, created_at, updated_at] 
      end

      csv << []
      csv << []
      csv << ["#{male_count}/#{Athlete.count} athletes are men"]
      csv << ["#{female_count}/#{Athlete.count} athletes are women"]
      csv << ["#{Athlete.count-male_count-female_count}/#{Athlete.count} athletes have not declared a gender"]
    end

    return csv_string
  end
end

这是从cron job rake任务调用的:

require 'csv'

namespace :reports do
  desc "Geo-report"
  task :generate_nightly => :environment do
    Report.create("Geo")
  end
end

不确定从何处开始实现此功能。有什么建议?我一直在阅读Paperclip的文档,但我对它有点新手。

谢谢!

1 个答案:

答案 0 :(得分:0)

这里有很多事情发生了:)

首先,看起来你的控制器和模型很混乱。在rake任务中,Report是模型,但您正在调用create,就好像它是控制器方法一样。模型(又名ActiveRecord类)采用键/值对:

Report.create(type: "Geo")

另一个问题是您使用“type”作为列的名称,这将告诉ActiveRecord您正在使用单表继承。这意味着您拥有Report的子类。除非您真的想要STI,否则您应该重命名此列。

最后,你不应该有一个带参数的控制器方法。我不确定你在那里做什么,但是控制器通过params hash得到他们的论点。