I am trying to learn how the Rails /lib/ directory works - and how to
reference variables defined in a file placed in the the /lib/
directory for use in a View.
I have a file called helloworld.rb and it’s saved in the /lib/
directory in my Rails application.
The helloworld.rb file has the following code:
module HelloWorld
def hello
@howdy = "Hello World!"
end
end
I want to be able to display the results of this method on a View
called index.html.erb, so I include the following code in the
index_helper.rb file:
module IndexHelper
require 'HelloWorld'
end
Also, I include the following code on the view index.html.erb:
<%= @howdy %>
I’ve read where I need to include the following line in the /config/
application.rb file:
config.autoload_paths += %W(#{config.root}/lib
However, my preference is to load this module only when the View
renders the page, not when the Application loads. What am I missing?
Thank you