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
Related
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'
My application has lib/project/errors which contains a bunch of Exception classes, one of which is ServiceException
module Project
module Errors
class ServiceException < Exception
def initialize(message = nil)
super message
end
end
end
end
I am trying to use this in my GameService:
module GameMan
class GameService
Blah blah
def validate(score)
raise Project::Errors::ServiceException.new('blah')
end
end
end
This works,
however I hate writing the full module path everywhere. Is there a way to avoid this?
I have tried
module GameMan
class GameService
include Project::Errors
Blah blah
def validate(score)
raise ServiceException.new('blah')
end
end
end
This gives
uninitialized constant ServiceException error.
I have
config.autoload_paths +=
%W(#{config.root}/lib #{config.root}/app/services)
already set inapplication.rb``
What am I doing wrong?
It is all about constants lookup.
ServiceException is defined in the scope of Project::Errors. When you reference ServiceException without prefixing Project::Errors it looks for the class defined in the outer scope, and failing, because there is none.
You should be using the full path.
include Project::Errors
Replace above line to following line
include Project::Errors::ServiceException
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.
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.
I'm trying to define a constant in an initializer file and to use it into a model.
config/initializers/constants.rb
DEFAULT_EVENT_DURATION = 15
app/models/event.rb
class Event < ActiveRecord::Base
before_validation :set_end_and_allday
[...]
def set_end_and_allday
self.allDay ||= false
self.end_event ||= self.start + DEFAULT_EVENT_DURATION.minute
end
end
However, when it try to create a new event, it displays the following error in the logs:
NameError - uninitialized constant Event::DEFAULT_EVENT_DURATION
Am I doing something wrong?
I've made some searches on google, but I didn't find any solution (except defining constant into the model and not in the initializer... and that's not what i want to do).
It was just a scope problem: constant was in the root scope but it was searching the constant in the controller scope.
A simple '::DEFAULT_EVENT_DURATION' solved the issue.