nil:NilClass 的未定义方法 'path'


require 'csv'
class Product < ActiveRecord::Base
has_attached_file :photo, :styles => { :small => "150*150>" }
def self.import(file)  
CSV.foreach(file.path, headers: true) do |row|  
product = find_by_id(row["id"]) || new  
product.attributes = row.to_hash.slice(*accessible_attributes)  
product.save!  
end  
end  
end    
class ProductsController < ApplicationController
   def import  
   Product.import(params[:file])  
   redirect_to root_url, notice: "Products imported."  
   end  
end

可能是params [:file]为零,因此该行失败:

CSV.foreach(file.path, headers: true) do |row|

相关内容

最新更新