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.
Related
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 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 have a Game model, and in my model, I want to use a helper class, GameBoard. I tried to create game_board.rb in app/helpers, and then use it in my game model.
#game_board.rb
class GameBoard
def initialize(foo)
#stufff
end
end
#in after_intialize in game.rb
#board = new GameBoard(foo)
However, when trying to create a game, it fails to create the GameBoard, saying
NoMethodError: undefined method `GameBoard' for Game:0x00000005309df0
I tried requiring game_board.rb at the top of the file and that didn't work. Does anyone know what I'm doing wrong?
Thanks.
All files from app/ directory automatically loaded by Rails. So, for instantiate a new object you should use (http://www.ruby-doc.org/docs/ProgrammingRuby/html/intro.html )
GameBoard.new(foo)
instead of
new GameBoard(foo)
In Rails you can create a model under app/foo/bar.rb, with bar.rb containing:
class Foo::Bar
def some_method
puts "I work fine"
end
end
If you try to do this in a pure ruby app you'd get a NameError: uninitialized constant Foo unless you've already initialized a module Foo.
What is Rails doing that allows it to create classes without first initializing their containing module? Is it possible to import this behavior through something like activesupport, or are we left to implement on our own?
Rails modifies the Class class to include a const_missing method which gets called when an undefined class is used. It then loads things to try and load the requested class.
The implementation of this in ActiveSupport is in lib/active_support/dependencies.rb.
actually model class created is extend to < ActiveRecord::Base
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.