Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Understanding the Rails /lib/ Directory
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
scottc  
View profile  
 More options Nov 6 2012, 3:49 pm
From: scottc <2scott...@gmail.com>
Date: Tue, 6 Nov 2012 12:49:16 -0800 (PST)
Local: Tues, Nov 6 2012 3:49 pm
Subject: Understanding the Rails /lib/ Directory
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frederick Cheung  
View profile  
 More options Nov 7 2012, 8:48 am
From: Frederick Cheung <frederick.che...@gmail.com>
Date: Wed, 7 Nov 2012 05:48:46 -0800 (PST)
Local: Wed, Nov 7 2012 8:48 am
Subject: Re: Understanding the Rails /lib/ Directory

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »