1

I am having trouble importing a spreadsheet using Rails and the roo gem and keep getting a bad URI(is not URI?) error. I think this is a simple question that I just can't figure out how to do, but how do I properly upload and access the file. Here is my current code:

controller.rb

def import
    if request.post?
      Spreadsheet.client_encoding = 'UTF-8'
      xls = Roo::Spreadsheet.open(params[:file])
      p s.cell(1,1)
    end
  end

html

<%= form_tag("", method: "POST", class: 'form-horizontal', multipart: true) do %>
  <div class="form-group">
    <%= file_field_tag :file, accept: 'xls,xlsx' %>
  </div>
  <div class="form-group">
    <%= submit_tag("Import", class: 'btn btn-default') %>  
  </div>
<% end %>
David Mckee
  • 1,100
  • 3
  • 19
  • 35
  • try this `Roo::Spreadsheet.open(params[:file], extension: :xlsx)` or `Roo::Spreadsheet.open(params[:file], extension: :xls)` – Roman Kiselenko May 04 '14 at 09:33
  • just tried that. I get this error: can't convert ActionDispatch::Http::UploadedFile into String. – David Mckee May 04 '14 at 10:11
  • 1
    you need pass path in `Roo::Spreadsheet.open(data.tempfile.path)` in my case, or something else. `params[:file]` contain file with metadata not only file. – Roman Kiselenko May 04 '14 at 10:14
  • You need to save file somewhere first, either via temp file as @Monk_Code proposed or using paperclip/ other gem. – Mike Szyndel May 04 '14 at 12:54
  • I want to use the tempfile, as I don't need the file anymore once it is uploaded. Data.tempfile.path does not work for me. How can I access that data variable? – David Mckee May 04 '14 at 20:22

1 Answers1

2

What I had to do was access the path through the params file. The code is below:

Roo::Spreadsheet.open(params[:file].path, extension: :xlsx)
David Mckee
  • 1,100
  • 3
  • 19
  • 35