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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Frederick Cheung at Nov 7, 2012 at 1:48 pm

    On Tuesday, November 6, 2012 8:49:16 PM UTC, scottc wrote:


    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?
    You're not so much missing what the lib folder does as misunderstanding
    what modules do. As an aside, loading stuff on demand isn't thread safe so
    usually best avoided in production.
    Just requiring a file doesn't do anything other than load that file. If you
    wanted to include a module then you'd need to do

    module IndexHelper
    include HelloWorld
    end

    in fact in your controller you could

    helper HelloWorld

    instead. Even this won't cause @howdy to be set - that would only be set
    when that hello method is called.

    Fred

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/vlTVJ0DkdfoJ.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedNov 7, '12 at 1:40p
activeNov 7, '12 at 1:48p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Scottc: 1 post Frederick Cheung: 1 post

People

Translate

site design / logo © 2021 Grokbase