Mascasc Mascasc - 1 year ago 94
Ruby Question

Rails CSV file upload problems

I'm trying to upload a CSV file(without headers) to a form in Rails, and then change my database based on that file's contents.

Now, the problem I'm having is that I know my routes must be correct, because I'm able to get to the correct method...but, my @imported variable is saying it is nil even when I upload a file.

Is there any help I can get for this? This will be my first file upload in rails, so we can cross this milestone together...

Here is the form in my view, mapped to the #ship action:

<%= form_for @import, :url => {:action => "ship", :controller => "imports"} do |f| %>
<%= f.file_field :import %>
<%= f.submit %>
<% end %>

And here is the corresponding method in my controller:

def ship
@import =[:file])
@import.each do |i|
Product.ship(@import[i][0]) #I believe the #read method imports
end #CSV files as an array of arrays
redirect_to "/" #But I have yet to get past the
end #first line, so I'm unsure

Answer Source

Sergio Tulentsev is right. You need to use params[:import][:import]. The first import being the name of your resource (comes from form_for @import, the second import being the field name (coming from f.file_field :import).

More info at

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