MongoId - uninitialized constant App::Mongo (NameError)" - ruby

In Sinatra app I have
#config.rb
require 'mongoid'
class App
configure do
Mongoid.configure do |config|
name = "my_db"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.slaves = [Mongo::Connection.new(host, 27017, :slave_ok => true).db(name)]
config.persist_in_safe_mode = false
end
end
end
#Gemfile
gem "mongoid", "~> 3.0.0"
gem "bson_ext"
and it gives me an error "`const_missing': uninitialized constant App::Mongo (NameError)"
How do I fix it?

This is due to the fact that Mongoid 3.x no longer uses the 10Gen Ruby driver so Mongo module will not be loaded by require 'mongoid'. You need to use the new Mongoid.load! method. You may have to change your config.yml file a bit as the syntax has changed. Please see http://mongoid.org/en/mongoid/docs/installation.html

Related

Sinatra CarrierWave Fog - NameError: uninitialized constant Fog

I have been trying to setup CarrierWave with Sinatra and Fog for S3 File Management. I constantly keep running into issues around Fog being undefined. This works fine the moment I change the storage to :file.
I have so far also tried solutions mentiohed here NameError: uninitialized constant CarrierWave::Storage::Fog and here NameError: uninitialized constant CarrierWave::Storage::Fog, heroku
But I have had no luck so far.
Here's my overall setup
Gemfile
gem 'fog', require: 'fog/aws'
gem 'carrierwave', '~> 2.0'
app.rb
require "carrierwave"
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'fog/aws', # required
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], # required unless using use_iam_profile
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], # required unless using use_iam_profile
use_iam_profile: true, # optional, defaults to false
region: ENV['AWS_REGION'], # optional, defaults to 'us-east-1'
}
config.fog_directory = ENV['S3_BUCKET_NAME']
config.fog_public = false # optional, defaults to true
config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
config.fog_provider = 'fog/aws'
end
Added this to my user class
mount_uploader :profile_picture, ProfileImageUploader
And lastly my ProfileImageUploader
class ProfileImageUploader < CarrierWave::Uploader::Base
storage :fog
end
I am still stuck at this output
NameError: uninitialized constant Fog
from ~/.rvm/gems/ruby-2.7.0/gems/carrierwave-2.1.0/lib/carrierwave/storage/fog.rb:159:in `connection'
try using
gem 'fog-aws'
and then
require 'sinatra/activerecord'
require 'carrierwave'
require 'carrierwave/orm/activerecord'
require 'fog/aws'

Builder's XmlMarkup object loses constants?

I try to upgrade a legacy application from Ruby 1.8.7 to 2.2.3. Afterwards the rendering of builder templates raises errors about unknown classes.
uninitialized constant Builder::XmlMarkup::BigDecimal (NameError)
It seem that within the Builder::XmlMarkup constants like classes disappear.
### example.xml.builder (template) ###
BigDecimal.new('23') # no error
class << xml
def some
data(BigDecimal.new('23')) # raises an error in 2.2.3
end
end
xml.test { xml.some }
### main script ###
require 'rubygems'
require 'builder'
require 'bigdecimal'
def eval_script(file)
xml = Builder::XmlMarkup.new
binding.eval(File.read(file), file)
xml.target!
end
template = File.join(File.dirname(__FILE__), 'example.xml.builder')
puts eval_script(template)
# Ruby 1.8.7 / builder 3.2.0 => <test><data>0.23E2</data></test>
# Ruby 2.2.3 / builder 3.2.2 => ./eval_script.rb:5:in `some': uninitialized constant Builder::XmlMarkup::BigDecimal (NameError)
I found no reason for the behavior. How can I fix the problem?
BTW: I have the same problem with the method lookup, e.g format('%d', 42) which returns the full XML document but doesn't call Kernel.format in Ruby 2.2.3.
I found a workaround overriding const_missing which has to be applied to every template file. It works so far for the legacy application.
### example.xml.builder (template) ###
class << xml
def self.const_missing(name)
super rescue ::Object.const_get(name)
end
def some
data(BigDecimal.new('23'))
end
end
xml.test { xml.some }
But every time the constant BigDecimal is used, it triggers const_missing and then raises a NameError and calls the Object method.

Factory Girl: NameError: uninitialized constant Vserver

I am trying to setup the unit testing with Factory Girl and Rspec for my Sinatra application.
Gem file:
group :test do
gem "rack-test"
gem "fuubar"
gem "factory_girl"
gem "yard"
end
spec/factories/vserver.rb
require 'factory_girl'
FactoryGirl.define do
factory :vserver do
first_name "John"
last_name "Doe"
end
end
spec/spec_helper.rb
require File.join(File.dirname(__FILE__), "..", "app.rb")
%w{
rubygems
sinatra
dm-core
rack/test
uuid
factory_girl
rspec
pp
spec/factories/vserver
}.each { |r| require r }
set :environment, :test
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec/app_spec.rb
require './spec_helper.rb'
require 'factory_girl'
describe "Cdot" do
include Rack::Test::Methods
def app
StorageApi
end
it 'vserver' do
FactoryGirl.build(:vserver)
end
end
Issue: When I run the rspec file using the command: rspec app_spec.rb, I get the below error.
NameError: uninitialized constant Vserver
Help is much appreciated.
the code: factory :vserver do assumes that you have a class called Vserver that you are instantiating.
If you have such a class, then you need to include it in your spec.
If you don't have such a class, then you either need to rename the factory, or tell it what class it should be instantiating instead.

Mongoid error in heroku: Database should be a Mongo::DB, not a nil class

I have a Sinatra app on heroku and it keeps crashing due to this error:
app/vendor/bundle/ruby/1.9.1/gems/mongoid-1.2.14/lib/mongoid/config.rb:52 in 'master': Database should be a Mongo::DB, not a nil class
I set up Mongoid 3.x according to the heroku instructions, and the app works on my local machine, so I'm not sure what's causing this problem. My gemfile looks like this:
source "https://rubygems.org"
ruby "1.9.3"
gem 'sinatra'
gem 'mongo'
gem 'mongoid'
gem 'bson_ext'
gem 'json'
gem 'nokogiri'
gem 'aws-s3', '0.6.2', :require => 'aws/s3'
gem 'sinatra-reloader'
gem 'debugger'
gem 'thin'
Here's my mongoid.yml:
development:
sessions:
default:
database: db
hosts:
- localhost:27017
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
skip_version_check: true
safe: true
And here's my app file:
require 'bundler/setup'
require 'sinatra'
require 'json'
require 'mongo'
require 'mongoid'
Mongoid.load!('mongoid.yml', :production)
def get_connection
return #db_connection if #db_connection
db = URI.parse(ENV['MONGOHQ_URL'])
db_name = db.path.gsub(/^\//, '')
#db_connection = Mongo::Connection.new(db.host, db.port).db(db_name)
#db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?)
#db_connection
end
db = get_connection
class Model1
include Mongoid::Document
field :name, :type => String
end
I shouldn't have to specify a database name since I'm using the uri field, so I'm not sure why the database if nil?

Mongoid gives uninitialized constant Mongo

I'm trying to use mongoid but it outputs this error:
uninitialized constant 'Mongo'
Here is my code:
require "mongoid"
Mongoid.configure do |config|
config.master = Mongo::Connection.new("localhost",27017).db("arthist")
end
class Artist
include Mongoid::Document
field :name, type: String
end
a = Artist.create(name: "hoge")
Do you have any idea?
include gem mongo in your Gemfile and restart the server it should do the trick.

Resources