Take the 2-minute tour ×

I have the following Ruby code which includes a form to select a file:

 <%= form_for :import, :url=>{:controller=>"user", :action=>"upload"}, :html => { :multipart => true } do |f| %>
        <div class= "field">
            <%= f.file_field :import %>
            <%= submit_tag 'Import' %>

Inside my controller I am trying to call the file that the user selected from their local disk.
The below code works when I give it the file:

book = Spreadsheet.open 'c:/myexcel.xls'

But I am not sure how to inside a controller call up the file that was selected from the form.
Please help

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Could do this with CarrierWave, see railscast here .

Heres the code to import data from an Excel spreadsheet:

def upload
    require 'spreadsheet'
    Spreadsheet.client_encoding = 'UTF-8'
    file_to_import = params[ :import ][ :import ]
    file = FileUploader.new
    book = Spreadsheet.open "#{file.store_path}"
    sheet = book.worksheet 0
    sheet.each 1 do |row|
        @user = User.new(:code => row[0], :name => row[1], :status => row[2], :account => current_user.account).save

Alternatively for uploading files and retrieving them you could use the 'paperclip' gem (railscast here ).

It is quite old but its a respected gem which is well supported and hasn't changed much.

Hope that helps

share|improve this answer
will try that with CarrierWave and the code you have given, thanks –  Damien Pyseek Jan 31 '13 at 14:18
yep works :) cheers –  Damien Pyseek Jan 31 '13 at 14:19
add comment

Your Answer


