Gerald Thomas
Gerald Thomas

Reputation: 3

uninitialized constant {model}Controller::Spreadsheet

I am using the Spreadsheet class inside my Rails (v2.0.0) controller code as follows:

def run
  book = Spreadsheet.open '--file path'
end

and I get a "NameError: uninitialized constant {model}Controller::Spreadsheet" error. The spreadsheet gem is loaded and I am able to execute this specific line in Rails Console.
When I added require 'spreadsheet' in the controller file as suggested in another Stackflow question I get the error 'unable to load module'. From my understanding of how GEM files should work, that is the only place I need to specify the gem. In my Gemfile I have gem 'spreadsheet' and I have run bundle install So why is the spreadsheet gem not available to the controller code? What am I missing?

Upvotes: 0

Views: 660

Answers (1)

paul h
paul h

Reputation: 324

I use Spreadsheet in a Rails app no problem.

Are you really using Rails v2.0.0?

My app has to handle XLS and PDF files, so I have the following setup:

app > builders > documents > xls_builder.rb

Then in xls_builder.rb

require 'spreadsheet'
module Documents
    module XlsBuilder

        def self.read_file path
            Spreadsheet.open path
        end
    end
end

When I want to open an xls file, I then just reference the Documents::XlsBuilder module and the methods inside.

Regards

Paul

Upvotes: 0

Related Questions