I am trying to upload a file to be parsed for attributes using semantic_form_for and I cannot seem to get it to work, although from what I've read it should be. I have been at this for a while now and cant seem to get anything to work.
Here is my view.haml:
= semantic_form_for(@user, :url => import_users_path, :html => {:multipart => true} ) do |f|
= f.inputs :id => 'inputs' do
= f.input :filename, :as => :file
%br
%br
= f.actions do
= f.action :submit, :label => 'BULK UPLOAD'
and the users controller import function:
def import
myfile = params[:filename]
CSV.foreach(myfile.path, headers: true) do |row|
@user = User.new(params[:user])
@user.name = row['Name']
@user.email = row['Email']
@user.save
end
redirect_to users_url
end
For whatever reason, the file does not seem to be passed into the import function. It worked fine with form_for, but not semantic_form_for which is what i need to try and use. Thanks for any help!
Just change myfile = params[:filename]
to myfile = params[:user][:file]
in your controller's import method:
def import
myfile = params[:user][:file]
CSV.foreach(myfile.path, headers: true) do |row|
@user = User.new
@user.name = row['Name']
@user.email = row['Email']
@user.save
end
redirect_to users_url
end