6

I use cloud9 ide to use ruby on rails!

I'm testing the gem 'roo' to bring excel file to my DB. before I do it, I wanted to test this gem work.

gem doc : https://github.com/roo-rb/roo
but there is a problem to bring file!

The error message is like this

IOError in MersmapController#index

file ../assets/test.xlsx does not exist


And here is my code!

require 'roo'

class MersmapController < ApplicationController         


  def index
      xlsx = Roo::Excelx.new("../assets/test.xlsx")
      @show = xlsx.info
  end
end

and in index.erb

<h1> <%= @show %> </h1>



I test this path using my "images.jpg" (the image file)

when I write path of an image file in index.erb it definitely works!!

I tried

xlsx = Roo::Excelx.new("../assets/excel/test.xlsx")

xlsx = Roo::Excelx.new("../../app/assets/test.xlsx")

xlsx = Roo::Excelx.new("../../app/assets/excel/test.xlsx")

...... All the things!!

but finally I couldn't figure out what is the problem... I appreciate if you help me out!!

pnuts
  • 58,317
  • 11
  • 87
  • 139
Hyung Kyu Park
  • 255
  • 2
  • 3
  • 12

2 Answers2

4

You can use Rails.root to get the path name of your file:

xlsx = Roo::Excelx.new(Rails.root.join('app', 'assets', 'excel', 'test.xlsx'))
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • 1
    In case of confusion with relative paths, you should always use absolute file path. Please consider upvoting the answer if it helped you solve your problem. Thanks :) – K M Rakibul Islam Aug 27 '15 at 16:07
  • oh I didn't know that there is a vote system thank you! – Hyung Kyu Park Aug 27 '15 at 16:08
  • In my case, the excel file is uploaded with the gem CarrierWave. This is what I used, and it's working. `xlsx = Roo::Excelx.new(Rails.root.join(@school.attachment.file.path))` – Lesego M Mar 13 '17 at 11:33
0

I remember that once happened to me, check if this solves your problem:

xlsx = Roo::Excelx.new(url_to_file, file_warning: :ignore)