Ran
Ran

Reputation: 3523

Uninitialized constant in application controller

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?

Upvotes: 1

Views: 4602

Answers (1)

rubyprince
rubyprince

Reputation: 17793

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.

Upvotes: 1

Related Questions