Reputation: 81
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?
Upvotes: 0
Views: 545
Reputation: 152
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...
Upvotes: 1