Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two ways that files get loaded in Rails:</p> <ul> <li>It is registered in the autoload process, and you reference a constant that corresponds to the file name. For instance, if you have <code>app/controllers/pages_controller.rb</code> and reference PagesController, <code>app/controllers/pages_controller.rb</code> will automatically be loaded. This happens for a preset list of directories in the load path. This is a feature of Rails, and is not part of the normal Ruby load process.</li> <li>Files are explicitly <code>require</code>d. If a file is <code>require</code>d, Ruby looks through the entire list of paths in your load paths, and find the first case where the file you <code>require</code>d is in the load path. You can see the entire load path by inspecting $LOAD_PATH (an alias for $:).</li> </ul> <p>Since <code>lib</code> is in your load path, you have two options: either name your files with the same names as the constants, so Rails will automatically pick them up when you reference the constant in question, or explicitly require the module.</p> <p>I also notice that you might be confused about another thing. ApplicationController is <strong>not</strong> the root object in the system. Observe:</p> <pre><code>module MyModule def im_awesome puts "#{self} is so awesome" end end class ApplicationController &lt; ActionController::Base include MyModule end class AnotherClass end AnotherClass.new.im_awesome # NoMethodError: undefined method `im_awesome' for #&lt;AnotherClass:0x101208ad0&gt; </code></pre> <p>You will need to include the module into whatever class you want to use it in.</p> <pre><code>class AnotherClass include MyModule end AnotherClass.new.im_awesome # AnotherClass is so awesome </code></pre> <p>Of course, in order to be able to include the module in the first place, you'll need to have it available (using either of the techniques above).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload