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.
Related
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 yo create a service object to extract a few methods from the product.rb AR model, but for some reason I can't autoload the new TwitterShare class. When I hit up the console and try something like Product.last.twitter_share_text I get NameError: uninitialized constant Product::TwitterShare error.
What's going on in here? How should I organize my folders/files? Do I have to tell rails to autoload services? Here is the current code:
app/models/product.rb
class Product < ActiveRecord::Base
def twitter_share_text
TwitterShare.new(name: self.name, oneliner: self.oneliner).return_text
end
app/services/twitter_share.rb
class TwitterShare
attr_reader .........
def initialize....
end
You need to let rails know where it could possibly find TwitterShare.
Add the following to your application.rb
config.autoload_paths << "#{Rails.root}/app/services"
and then restart the console or server.
rails should now be able to locate twitter_share.rb and load TwitterShare correctly.
Refer to Autoloading and Reloading Constants for more info.
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
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 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.