Rails - Unable to use gem in controller - ruby-on-rails

I am attempting to use the Roo gem to parse a spreadsheet file in my Rails controller. I have added the appropriate line to my Gemfile and ran the bundle command. Here is the line in my controller where everything breaks:
xlsx = Roo::Spreadsheet.open(filepath)
I receive an error: uninitialized constant MyController::Roo. I then added the following line to the top of my controller file (as specified in the Roo readme):
require 'roo'
Now I get a different error: Error during failsafe response: cannot load such file -- roo.
What am I doing wrong?

Did you bundle and restart your server?
Adding gems, modifying app/config files or app/lib files require a server restart :)

Related

What can be done to fix the following error when we run any rails command: " `require_relative': cannot load such file "

Any rails command doesn't work for me. I have several versions of ruby installed through rvm. I tried installing rails with all the versions, they do install successfully but with all of them I face the following error whenever I run any rails command in my project directory:
~ rails new blog
Traceback (most recent call last):
1: from bin/rails:3:in `<main>'
bin/rails:3:in `require_relative': cannot load such file -- /Users/Am33d/Documents/config/boot (LoadError)
I tried looking up for the error but didn't find any solutions.
What can be done to fix this? I am using macOS Mojave (10.14.6)
This error would indicate that you do not have a boot.rb file in your config directory for some reason. When running rails commands -- regardless of if you run them as bin/rails [command] or bundle exec rails [command], runs the bin/rails file. This file typically has a line require_relative '../config/boot. The boilerplate bin/rails file in a new Rails 6 app is:
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
To simply fix this you can create a blank file by running touch config/boot.rb from the root directory of your application and that would itself suppress the error. Having said that, you'd be better off creating a config/boot.rb file that contains useful information. The boilerplate config/boot.rb file is this
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
Without this file you are not necessarily appropriately loading the gems from your gemfile or caching operations with bootsnap.
When I ran into this problem, I also received the same error when trying to use rails -s.
If the same is happening for you, its because your version of ruby isn't compatible with your rails version.
After upgrading to the latest rails version the error stopped and everything worked well.
A little bit weird.
rails new blog should not need to find boot file, neither need bundler. Actually, you are trying to create a new project, so it is expected there is not Gemfile, boot, or bundler created for your project.
So I would advise you to find what rails command is being executed. Sometimes one rails executable created by bundler is taking precedence in the $PATH environment variable.
Ex: some incorrect bin/rails executable is on your $HOME directory.
i was trying to build an app using 'rails server' and this error was showing. You have to make sure that the rails version matches the ruby version and even if you do what an answer above said (create the config/boot.rb paste) doesnt work, than you have to change the version of rails to the stable. I'll let the link of this problem here, there's an issue closed in github for this problem https://github.com/rails/rails/pull/43951
to solve the problem you have to replace the gem rails line on the gemfile to this:
gem "rails", github: "rails/rails", branch: "7-0-stable"
Sorry about my english.

Rails: Cannot load such file -- ordrin

I am still new to development using Rails, and have followed various tutorials. I want to create a simple application using the Ordrin API. I have been stuck at a minute problem for a long time.
Using the Ordrin API makes use of the line:
require 'ordrin'
However, the view related to the controller this occurs in shows the following error.
cannot load such file -- ordrin
Even though I have installed the gem earlier using
gem install ordrin
I have tried using the complete path for the gem returned by the command
gem which ordrin
But then I get an error cannot load such file -- json-schema, which is apparently a dependancy for ordrin.
How do I make the require statement load the default gem as managed by RubyGems?
add the following line in your gem file and try
gem 'ordrin'
and don't forget to run the following command
bundle install

How to read files (.yml) from a Gem

I want to read a .yml file included in a Rails Engine Gem from my main_app. The file is located at config/test.yml.
IO.read("config/test.yml") fails with No such file or directory # rb_sysopen
If i move the file into the main app, everything works fine. But i need this file in a Gem.
Solution 1
You can store the root path in a constant from your main gem file and retrieve it in other locations of your code. You must ensure that the gem got initialized before the code in your app runs otherwise you'll have an errors because the constant won't be defined.
# lib/my_gem.rb
GEM_ROOT = File.expand_path("../..", __FILE__)
# app/.../some_class.rb
IO.read("#{GEM_ROOT}/config/test.yml")
Solution 2
The most advisable, you can get the gem path programmatically from Bundler, then use that root path to retrieve the full path of your yml file.
Have a look at this answer that you can easily adapt to your case
You should be able to load app yaml files by doing this in your gem:
YAML.load_file('config/test.yml')

uninitialized constant CarrierWave::Video

Error message of my server
A NameError occurred in..
uninitialized constant CarrierWave::Video app/uploaders/video_uploader.rb:7:in
<class:VideoUploader>'
video_uploader.rb:7 => include CarrierWave::Video
INFO :
In my local, it works(development & production mode)! But after deploy to server, occur that error.
But in my server rails c production,Input 'include CarrierWave::Video' then Rails Console print not 'NameError: uninitialized constant..' but 'Object'. This means 'include CarrierWave::Video' succeeded!!
Of course, these gem(gem 'carrierwave' , gem 'carrierwave-video') is in my Gemfile and Gemfile.lock and bundle install success.(when deploy) So in my_app/shared/bundle/ruby/2.0.0, GEM_HOME & GEM_PATH of my app, exist these gems
I think 'include' doesn't work. because Like this, i have been met this error at line 'include CarrierWave::RMagick' when using gem 'rmagick' to resize image file uploaded.
Of course, this is not solved yet..
Purely CarrierWave removed line 'include ~~' works well (ex : Image upload not resized..)
nginx + unicorn, linux ubuntu 12.04
Please help me.. T^T
i dont know why 'include CarrierWave::Video' not work in .rb file but work in rails console..
ADDED :
'require' about these gems not exist in my code(in application.rb.. etc..),
(require 'carrierwave/video'.)
If add "require 'carrierwave/video'" to application.rb below require 'rails/all',
occured my application is now worked all. If add to video_uploader.rb and I request page using video_uploader , then occur error cannot such file bulabula..
This error caused by your server didn't reload the module.
Just restart your rails server, and everything will go right.
Make sure you are loading CarrierWave after loading your ORM.
If you added gem 'carrierwave' do this rails generate uploader UPLOADER_NAME

Rails/Ruby/Postgres - LoadError cannot load such file -- pg_ext

I am trying to call a Ruby script (which connects to a postgres db) using the rails controller below, however it appears it is having problems loading one of the PG gem files. I have set my require statement to require 'pg' and tried the absolute path as well (require /usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/). The file 'pg_ext' is in fact present in the directory. Additionally, I can run the ruby script standalone without any problems (dbrubyscript.rb), however when rails is added to this equation it craps out with a cannot load such file -- pg_ext error.
Any direction here would be much appreciated as I have not been able to find anything online that fixes this issue
Rails controller:
class TestdlController < ApplicationController
def runmyscript
load "/usr/local/rvm/my_app/ruby_scripts/reports/dbrubyscript.rb"
send_file '/usr/local/rvm/tmp/failedtests.csv', :type => 'text/csv', :disposition => 'inline'
flash[:notice] = "Reports are being processed..."
end
end
.rb file (dbrubyscript.rb) has the following:
require 'rubygems'
require 'pg'
connects to (production) database
#conn = PGconn.connect("zzzzz.test.prod", 5432,"","","yyyyy_prod" ,"postgres", "xxxxxx")
.....
Trace Error:
LoadError in TestdlController#runmyscript
cannot load such file -- pg_ext
Rails.root: /usr/local/rvm/my_app Application Trace | Framework Trace
| Full Trace app/controllers/Testdl_controller.rb:3:in `runmyscript'
This error occurred while loading the following files:
/usr/local/rvm/my_app/ruby_scripts/reports/dbrubyscript.rb
/usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/
pg_ext
Try running ruby /usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/ext/extconf.rb and see what errors you get. That helped me determine that in my case, my PostgreSQL client was too old. Your error may be different since you appear to have a current-ish versioninstalled.
I had the same problem.
I have a stand alone ruby script. It connects via pg to postgres and worked if I ran it directly from the shell.
If I tried to run it via rspec I get the Error cannot load such file -- pg.
Solved: The problem for rspec was, the pg gem was not defined in the Gemfile. After put pg into Gemfile, and retestet via rspec, it worked.
Try adding a line to your Gemfile:
gem "pg"
Then run bundler via the command line:
bundle install
Rails uses Bundler to manage your gems and dependencies. You can read a bit more about the idea behind Bundler here: http://gembundler.com/v1.2/rationale.html

Resources