def import
@samples = Sample.all
Sample.import(params[:file])
redirect_to root_url, notice: "Samples Imported"
authorize @samples
end
这是我的模特......
class Sample < ActiveRecord::Base
belongs_to :user
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Sample.create! row.to_hash
end
end
end
有什么建议吗?
答案 0 :(得分:1)
您可以在控制器中查看if params[:file].present?
。
答案 1 :(得分:0)
如果您使用Paperclip将此行添加到附件模型中 class Attachment&lt;的ActiveRecord ::基
validates_attachment_presence :attach, :message => "No file selected"