venu
venu

Reputation: 813

undefined method `path' for nil:NilClass

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

Upvotes: 0

Views: 2300

Answers (1)

Avi Tevet
Avi Tevet

Reputation: 828

Probably params[:file] is nil, and therefore this line is failing:

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

Upvotes: 1

Related Questions