I am trying to create an instance of class in a rails model
/app/models/employee.rb
class Employee < ActiveRecord::Base
def self.import(file)
preferences = ::MotionlessAgitator::EmployeeAvailability.new
...
end
end
except it does exist in:
/app/models/motionlessagitator/employeeavailability.rb
module MotionlessAgitator
class EmployeeAvailability
def initialize(csv_name = nil)
I am being given this error:
NameError (uninitialized constant MotionlessAgitator):
app/models/employee.rb:5:in `import'
app/controllers/employees_controller.rb:65:in `import'
Have tried calling with/without the "::" and from within the controller. I'm still fairly new at this and not exactly sure how the load paths work though
You are missing underscores in directory and file names. In order to get your class loaded automatically, you should have it in app/models/motionless_agitator/employee_availability.rb file.
Related
In my Rails 5 app I have a module in app/lib
module LibClass
CONSTANT_NAME = ‘somevalue’
end
Then in a model I reference the module:
class SomeModel < ApplicationRecord
def lib_class_constant
LibClass::CONSTANT_NAME
end
end
Everything works as expected when I call lib_class_constant on an instance of a SomeModel in console
But if I do the same in a view:
<%= some_model_instance.lib_class_constant %>
I get an error along the lines of:
uninitialized constant SomeModel::LibClass
If I reference the module directly in the view:
<%= LibClass::CONSTANT_NAME %>
I get an error along the lines of:
uninitialized constant ActionView::CompiledTemplates::LibClass
What am I missing here?
Have you tried the line include LibClass right after class SomeModel < ApplicationRecord ?
Otherwise have you checked that models and files have the right names? i.e. sometimes you rename a model without renaming the file accordingly or vice versa...
I have model as follows
app/models/views/def_usage.rb
class Abc
class Def < ActiveRecord::Base
self.table_name = 'vSomeview'
end
end
I am trying to create a factory girl for this
spec/factories/views/def_usage.rb
FactoryGirl.define do
factory :def_usage, class: Abc::DefUsage do
......
end
end
I am getting error uninitialized constant Abc::DefUsage (NameError)
I tried changing class: Views::Abc::DefUsage or Views::DefUsage but no luck. i am getting that error when i am trying to do rails console. why i am getting that error?
Your path needs to match your module/class hierarchy.
If you want your class to be in app/models/views/def.rb, then your class needs to be Views::Def.
If you want your class to be Abc::Def, your path needs to be app/models/abc/def.rb.
If you want your class name to be DefUsage, your file name needs to be def_usage.rb.
You can't use arbitrary paths and class names. They need to match if you want Rails to automatically load constants for you.
I'm trying to create a new model in spree extension. I generated a model and it is in /spree_extension/app/models/my_class.rb:
module Spree
class MyClass < Spree::Base
belongs_to :product
end
end
But when I start my application, there is no Spree::MyClass, I get this error:
NameError: uninitialized constant Spree::MyClass
I tried moving my_class.rb to "spree" directory, but nothing helps.
Most probably, you need to put your class into:
/spree_extension/app/models/spree/my_class.rb
As rails is always expecting to find classes inside file with the same name, inside folder that has the module name.
The problem was in fact I created a table my_class.
Since I renamed it to spree_my_class, it works.
I'm trying to model some UI elements.
I've created a series of classes in /app/models/wrappers/*
For this post i'm going to focus on a class called InputTextVO
I have:
class InputTextVO
...
end
/app/models/wrappers/InputTextVO.rb
When I try and initialize it in my controller I get the following:
NameError in InputsController#index
uninitialized constant InputsController::InputTextVO
#ivo = InputTextVO.new
RubyMine can locate the class and doesn't report any errors in my controller.
You must to add a module if you want to create a subdirectory in the model directory. You can do it like this :
class Wrappers::InputTextVO
...
end
Wrappers::InputTextVO.new #....
It should work.
You can also create a new directory like this app/wrapper.
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.