noob
noob

Reputation: 81

Rails 5: Uninitialized constant in view/controller but not console

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

Answers (1)

Antoine Quellier
Antoine Quellier

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

Related Questions