Uninitialized constant in module in controller - ruby-on-rails

I have this controller
module GaReporting
module Api
class GaGatheringController < ApplicationController
client = Google::APIClient.new(...)
end
end
end
However I get an uninitialized constant error for the
uninitialized constant GaReporting::Api::GaGatheringController::Google
This is curious since this line works fine when I just call it in a "normal" controller that is not inside any modules.
How do I fix this and why is it not working?

adding require 'google/api_client' in the class does the trick. Interesting that in the module the require statement is -- pardon the pun -- required since in a regular controller it is not.

Related

Ruby - NameError: uninitialized constant. Using class from a different module

I have these 2 files in a large system, both are located in PackA
people.rb
module People
class HobbyValueObject
end
end
job.rb
module People
class Job
end
class CityValueObject
end
end
I am trying to use CityValueObject in a different module like this,
Module is in PackB
work.rb
module Profile
class Work
....
def calculateTaxes
...
a = People::CityValueObject....
end
end
end
But it's giving me an error saying,
NameError: uninitialized constant People::CityValueObject
Did you mean? People::HobbyValueObject
Why is not being able to fine CityValueObject but can find HobbyValueObject just fine?
How do I make it find the object that I am intending to use?
I am not explicitly declaring any requires or includes
I was able to resolve this by adding require at the top while using full path file name.
require './packs/people/app/public/people/job'

Accessing helpers and models from rails engine initializer

I'm trying to make a Ruby on Rails engine, and I want the initializer to be able to have access to the helpers and models.
I'll write below an example, part of the code, and the error that I have. It may not be the recommended way, because I can see that in some cases I'm repeating myself, but it's the first engine I make.
file lib/my_engine/engine.rb
module MyEngine
require 'my_engine/functions'
class Engine < ::Rails::Engine
isolate_namespace MyEngine
config.autoload_paths += %W( #{config.root}/lib )
end
class GlobalVars
attr_accessor :foo
def initialize
#foo = MyEngine::Functions.new
end
end
class << self
mattr_accessor :GLOBAL
mattr_accessor :USER_CONFIG
self.GLOBAL = MyEngine::GlobalVars.new
# add default values of more config vars here
self.USER_CONFIG = 'default config'
end
def self.setup(&block)
yield self
end
end
file lib/my_engine/functions.rb
module MyEngine
require '../../app/helpers/my_engine/options_helper'
class Functions
include MyEngine::OptionsHelper
attr_accessor :some_link
def initialize
#some_link = get_option('dummy')
end
end
end
There is also a controller named OptionsController in app/controllers/my_engine, and OptionsHelper in app/helpers/my_engine/options_helper.rb:
module MyEngine
module OptionsHelper
def get_option(name)
MyEngine::Option.new
end
end
end
When I try to run the dummy application, this error occurs:
/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::Option (NameError)
If I change to just Option.new, I have this error:
/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::OptionsHelper::Option (NameError)
For ::MyEngine::Option.new, I have:
/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::Option (NameError)
For ::Option.new, I have:
/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant Option (NameError)
The dummy application has nothing in it. All helpers and models defined above are in the engine.
Before this, I had other errors because it couldn't access the helper, or the Functions class. I had to add require and include to make it work even if they are placed in the same directory. Also, to work, I had to move GlobalVars from its own file inside engine.rb.
Can somebody show me what I'm doing wrong?
After I used required for every class, I ended with ActiveRecord::ConnectionNotEstablished, and it seems that not everything is loaded and available at that point when the GLOBAL object is created.
So I moved the code that was using the models in a separate init method. Then, I added an after initialize event:
config.after_initialize do
MyEngine.GLOBAL.init
end
I see a possible problem: because you are inside module MyEngine it might be possible that actually rails is looking for MyEngine::MyEngine::Option, so I see two approaches:
just write Option: this will look for MyEngine::Option
write ::MyEngine::Option this will look in the global namespace and find MyEngine::Option
Secondly, if that does not help, even though your path seems correct, but you can always explicitly require "my_engine/option" at the top of the file. I am not entirely sure the autoloading in an engine works in quite the same way, and I tend to, in my engine file, require almost everything (to make sure it works).
In my engine.rb I do
require_relative '../../app/models/my_engine/option'
maybe this will help, but it is not a nice solution.

Uninitialized Constant in Rails Controller

I have the following in my controller:
class SurveysController < ApplicationController
def index
survey_provider = FluidSurveysProviders::SurveyProvider.new
contact_lists = survey_provider.get_lists()
#survey = Survey.new(contact_lists)
end
And I'm receiving this error:
NameError in SurveysController#index
uninitialized constant SurveysController::FluidSurveysProviders
Excuse my Rails noobiness, I'm sure I'm leaving out something important here. But it seems to me that I am trying to "initialize" the constant with this line:
survey_provider = FluidSurveysProviders::SurveyProvider.new
But that's the same line that's throwing an error because it's not initialized. Where should I be "initializing" the Provider?
Once you require fluid_surveys_providers (or similar) then do this:
include FluidSurveysProviders
Make sure SurveyProvider is wrapped with module FluidSurveysProviders. It may look like this
module FluidSurveysProviders
class SurveyProvider
...
end
end
if its an ActiveRecord object try this
class FluidSurveysProviders::SurveyProvider < ActiveRecord::Base
...
end
The SurveyProvider was not loaded correctly.
For a quick fix, move the class file into app directory, e.g. app/lib/survey_provider.rb. Then all code inside app will be auto-loaded by Rails.
Or make sure the path to class SurveyProvider is included in the autoload_path of Rails. In config/application.rb
config.autoload_paths += %W(#{config.root}/lib) # where lib is directory to survery_provider
If you use Rails 5, be careful that autoload is disabled in production environment. Check this link for more info.

Getting "uninitialized constant" in Rails app

I'm new to Rails and feeling my way, but this has me stumped.
I moved some constants to a separate module ie:
module Fns
Fclick = "function() { alert(\"You clicked the map.\");}\n"
...
end
then in my controller added:
require "fns"
class GeomapController < ApplicationController
def index
fstring = Fns::Fclick
...
end
but when I run the server I get:
uninitialized constant Fns::Fclick
what am I missing?
Works for me if the module is in lib/fns.rb.
For the sake of convention use all-caps for constant names: http://itsignals.cascadia.com.au/?p=7

Accessing a Constant Defined in ActiveRecord::Base

I am trying to access the constant VALID_FIND_OPTIONS defined in ActiveRecord::Base (active_record/base.rb Line 2402 Rails 2.3.5).
ActiveRecord::Base::VALID_FIND_OPTIONS
I get the NameError exception.
NameError: uninitialized constant ActiveRecord::Base::VALID_FIND_OPTIONS
I have accessed the class constants in other libraries using the similar syntax before. I am not sure where I am going wrong.
The constant VALID_FIND_OPTIONS was defined inside the anonymous class of ActiveRecord::Base, hence it was not accessible as ActiveRecord::Base::VALID_FIND_OPTIONS
module ActiveRecord
class Base
class << self
# the constant belongs to the scope of the anonymous class
VALID_FIND_OPTIONS = [..]
end
end
end
The constant can be accessed using following syntax:
ActiveRecord::Base.singleton_class::VALID_FIND_OPTIONS
Where is the code that tries to get ActiveRecord::Base::VALID_FIND_OPTIONS?
If you are defining a class before ActiveRecord is loaded, then the constant will not be available.
You can force ActiveRecord to be loaded by requiring it. In some cases, you will have to require rubygems before requiring active_record.
Try requiring them both:
require 'rubygems'
require 'active_record'
# you should now be able to access ActiveRecord::Base::VALID_FIND_OPTIONS

Resources