First, write a view to upload your file. You can use Paperclip for this.
Assuming you have a resource Csv
, your upload form could look like this:
<%= form_for @csv, :url => csv_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :attachment %>
<% end %>
Your model:
class Csv < ActiveRecord::Base
attr_accessible :attachment
has_attached_file :attachment
end
Your controller actions:
def create
@csv = Csv.create( params[:csv] )
# your save and redirect code here
end
def show
@csv = Csv.find(params[:id])
end
Having that, you can use something like this in your view:
CSV.foreach(@csv.attachment.path) do |row|
# use the row here to generate html table rows
end
Note: this is just a general idea of how this can be done and you need to have the the resource added to your routes, Paperclip gem installed and configured, etc – read the doc on how to do all that.
3
solved Rails upload CSV file with header