2014-01-25 29 views
0
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 

回答

1

大概PARAMS [:文件]是零,因此,该线失败:

CSV.foreach(file.path, headers: true) do |row| 
相关问题