Lut Lut -4 years ago 147
Ruby Question

CSV Import: undefined method `path'

I am trying to import a CSV with addresses. I am requiring 'csv' at the controller. However I get this error:

NoMethodError in AddressesController#import_addresses

undefined method `path' for "testimport.csv":String

Address.import(params[:file].path)


Address Controller

class AddressesController < ApplicationController

require 'csv'

def import_addresses
Address.import(params[:file].path)
redirect_to root_url, notice: "Addresses imported."
end


Address Model

def self.import(file)
CSV.foreach(file, headers: true) do |row|
Address.create! row.to_hash
end
end


Reference: http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

Answer Source

please try this one form_tag(import_addresses_path, :multipart => true) do * The multipart option is not part of the url_for options. so you have to make them explicitly separate* so the form_tag isn't generated right and string is sent to controller, not the object.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download