Hello,
I need some help trying to figure out how to add the ability for a
user to upload a text file, and then have the application parse that
text file, and add each record as instance of a model object. I’ve
found a lot of helpful articles out there, but I’m stuck trying to
bring it all together since I’m still very new to all of this.
Here’s my view code:
index.html.erb
<% form_for :upload, :url => { :action => :upload }, :html =>
{ :multipart => true } do |f| %>
<p>Please select a file to upload:</p>
<%= f.file_field :file %>
<%= f.submit 'Upload', :disable_with => "Uploading..." %>
<% end %>
In my controller, I’ve been able to write something like this to work
with the data in my upload view:
mailing_lists_controller.rb
def upload
file = params[:upload][:file]
@data = file.read
end
so then the view looks like this:
upload.html.erb
<% @data.split("\r\n").each do |row| %>
<%= row.split("\t").join(" | ") %>
<% end %>
So, where I’m stuck is translating that into saving each row as a
record in the database. I’ve tried several different things in the
model (like creating a “process” method or using “before_save”), but
nothing has been successful thus far (probably because I’m not
implementing it correctly).
Any help would be appreciated.
Thanks,
Spencer