Rails/Ruby/Postgres - LoadError cannot load such file -- pg_ext - ruby-on-rails

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

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.

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

NameError in PostsController#pingback

This is my first time trying to get a Gem to work in rails where I haven't just been able to follow the documentation.
I first installed the gem using sudo gem install ping back and then added it to my Gemfile via gem 'pingback'. I then ran bundle install and it shows it installed in the list it outputs.
So then I wrong a little function that looks like this and is in my posts controller:
def send_trackback(posts)
posts.each do |post|
source_uri = "http://example.com/posts/#{post.slug_url}"
target_uri = post.target_url
Pingback::Client.new.ping(source_uri, target_uri)
end
end
whenever I try to load the admin page that sends the trackbacks I get the following:
NameError in PostsController#pingback
uninitialized constant PostsController::Pingback
Do I have to do more than just install the gem via bundler and then plug and play?
Update
adding require 'pingback' to the top of my posts controller results in this:
cannot load such file -- pingback
The error message to me indicates that the VM is trying to find PingBack in PostsController, I am thinking you are missing a require or include statement for PingBack.
It may be a typo, but pingback needs to be one word, not 'ping back' for the line in the gemfile, and for the gem install.
I would try running 'bundle list' to make sure the gem is installed.
I restarted the rails server and I believe this has resolved this issue.

Trouble including httparty in ruby on rails

I've been trying to use HTTParty in my rails code
sudo gem install httparty
From the command line I can now successfully do
httparty "http://twitter.com/statuses/public_timeline.json"
When I try this in my rails app
require 'rubygems'
require 'httparty'
class FooController < ApplicationController
include HTTParty
def bar
blah = HTTParty.get("http://twitter.com/statuses/public_timeline.json")
end
end
I get the error message "no such file to load -- httparty"
I suspect there is something wrong with my environment?
You don't need to do 'include HTTParty' inside the Controller. Just remove that and it should work. I just tested it and it worked for me. If this doesn't work for you, you should add the gem to your environment.
Usually if you use a gem inside your Rails application, you should add the following to environment.rb:
config.gem "httparty"
The gem will be available in the application now and you don't need to add 'require' inside the Controller. Also, you don't need to require RubyGems inside a Controller.
When you use Rails 3, you need to put the following inside the Gemfile:
gem "httparty"
I hope it works for you. :)
The problem is, if you load a new gem, you have to restart the server even if you are in development.
I had this same error. I tried moving the require HTTParty all over, but found, all I needed to do was restart the rails server In the end I did not need to 'require HTTParty' nor 'include' it. It just needed to be loaded into rails.
1)include the httpary in your gemfile
open your gem file then add
gem 'httparty','YOUR VERSION NUMBER'
2) run bundle install in your command prompt of the app file
3) restart the server
Ran into the same problem. Then I switched from Ruby 1.8.7 to Ruby 1.9.2 and all errors varnished into thin air.
(Yes, it first took me quite some hours to come up with the possibility that the Ruby version might be the problem. Configured a secundairy server to avoid possible conflicts with 2 ruby versions, and after way to many hours I got my RoR stack up and running. And the first test with httparty (based on the example on top) worked out of the box! Finally can sleep RESTfully again :-)
I run into the same error whilst reviewing a project from a student, I change the name of the Gem from uppercase to lowercase then run bundle install. I then went ahead to change the format in which they were being imported from
require 'HTTParty' to require 'httparty' and boom it worked

How do I get an installed ruby gem included in Rails?

I am attempting to get a gem I installed working in a Rails application. I can require the gem just fine in a Ruby program that I run from the command line using:
require 'nokogiri'
But when I attempt to do the same in one of my Rails controllers it errors saying "no such file to load -- nokogiri".
I tried using the full path to the lib/nokogiri.rb file, but that fails because it cannot find "nokogiri/native".
Better, place the following in your environment.rb file:
Rails::Initializer.run do |config|
...
config.gem :nokogiri
...
end
This will tell Rails that you depend on that particular gem. It also allows you to specify particular versions, and it will automatically keep all your gems synched, or unpack them into vendor/gems if you so wish.
I had a similar error but simply forgot to put the following in my environment.rb file: (note the quoted "nokogiri")
Rails::Initializer.run do |config|
...
config.gem "nokogiri"
...
end
Ok I figured it out. This is going to sound pretty stupid...but oh well...
It turns out I had two installations of ruby on my machine. I use InstantRails to serve my test applications and it comes prepackaged with an installation of ruby. I had another installation however outside of this and it was here that nokogiri had been installed, not in the installation in InstantRails.
In any case they were looking in different spots for the gems.
Try the following
require 'rubygems'
gem 'nokogiri'
If you are on some form of *nix then did you get any errors when you installed the gem, particularly errors stating that the gem was not on the path. This may happen if you have installed the gem as yourself rather than as root and you do not have your personal gem library in your gem path.
If you always install your gems using
sudo gem install some_gem_name
then you should not get that problem.

Resources