Uninitialized constant in application controller - ruby-on-rails

I have a model class in my Rails application called: UserAction
In that model I have a constant (which I use as an enum):
class UserAction < ActiveRecord::Base
end
class UserActionType
ACTION1 = "action1"
ACTION2 = "action2"
end
when I try to use that constant:
if some_action == UserActionType::ACTION1
in the application controller I get the following error:
NameError (uninitialized constant ApplicationController::UserActionType)
Any thoughts?

For getting the UserActionType class, actually you need to require the file in which it is written. For models or wherever autoloading is configured, Rails makes it easier by autoloading files. For example, if UserAction is encountered, Rails look for a file called user_action.rb in models or wherever you have autoloading configured and require that file automatically. So, in your case, you can make a new file called user_action_type.rb in app/models and paste your UserActionType class there. Then, this error will not occur.

Related

Accessing namespaced class in gem vs Rails

I've worked with a couple of Ruby gems and also Rails. One thing I've never fully understood is why Rails required explicit class constant references for code defined in the /lib folder. In a ruby gem, I could create something like this:
lib/my_gem/custom_error.rb
module MyGem
class CustomError < StandardError
end
end
lib/my_gem/some_class.rb
module MyGem
class SomeClass
def initialize
raise CustomError
end
end
end
Whether it's an error, another class or whatever, as long as the calling class is in the same namespace as the referenced class, ruby would initialize the correct class CustomError in the case above. Moving to Rails, this is a different story and this code would result in an uninitialized constant error. In Rails I would have to raise MyGem::CustomError instead. Why is this the case? I assume it has something to do with autoloading. Is there a way around this or is this standard?

Use model of diiferent name in a different controller

I have a controller FormsController and a Active Model ApplicationForm. I have made this model by including include ActiveModel::Validations, include ActiveModel::Conversion, extend ActiveModel::Naming. When I'm using #form=ApplicationForm I'm getting NameError uninitialized constant FormsController::ApplicationForm. How do I resolve this issue, I just want to use this Model as class which can provide object to hold form values temporarily for further processing.
Here is how to use it from a directory. Assuming it is in a directory app/forms and you have done correctly added the forms to the autoload paths for rails.
#form = ::Forms::ApplicationForm.new(your_params)
Your module will have to be named like this
module Forms
class ApplicationForm
#your form code
end
end

New model in Spree extension: uninitialized constant Spree::MyClass

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.

Ruby Wrapper Class results in NameError uninitialized constant

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.

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.

Resources