Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just ran into this as well. There are (at least?) 2 possible problems:</p> <ul> <li><strong>Your module is not in the autoload path</strong></li> </ul> <p>Look in <code>config/application.rb</code> for this line:</p> <pre><code>config.autoload_paths += %W(#{config.root}/extras) </code></pre> <p>If it's commented, uncomment it. This line will turn on autoloading for all files inside <code>extras</code>, and all files in subdirectories of <code>extras</code>, too. It's probably safest to move your modules into <code>extras</code>, but if you really want to leave them in <code>lib</code>, change the line to be:</p> <pre><code>config.autoload_paths += %W(#{config.root}/extras #{config.root}/lib) </code></pre> <ul> <li><strong>Your module is in the autoload path, but not named the way Rails expects</strong></li> </ul> <p>(see this: <a href="https://stackoverflow.com/questions/2796027/rails-2-3-5-how-does-one-access-code-inside-of-lib-directory-file-rb">Rails 2.3.5: How does one access code inside of lib/directory/file.rb?</a>)</p> <p>By convention, Rails wants the name of your module to match the directory hierarchy and the filename. So the file <code>extras/mylib.rb</code> would be expected to contain</p> <pre><code>module Mylib # not MyLib or My_lib ... end </code></pre> <p>This works for subdirectories as well, so a file <code>extras/mydir/mylib.rb</code> should contain:</p> <pre><code>module Mydir module Mylib # or class Mylib ... end end </code></pre> <p>This naming convention is the same as what Rails expects for controllers and models. Underscores in the filename turn into a camelcase class/module name. A file called <code>my_lib.rb</code> would be expected to have a <code>module MyLib</code> in it (but not <code>Mylib</code>).</p> <p><strong>NOTE</strong> that <em>autoload</em> does not mean that the module is automatically loaded at startup; rather, it's automatically loaded when it's first used. So even if you have some code like <code>puts "hi from mylib"</code> at the top of your <code>mylib.rb</code> file, you won't see that print until your code uses <code>Mylib</code> somewhere.</p> <p>Finally, if you really want your modules to load <em>at startup</em>, go create a file called <code>config/initializers/force_load_libraries.rb</code> and put this in there:</p> <pre><code>Dir.glob("#{Rails.root}/extras/force_load/*.rb").each { |f| require f } </code></pre> <p>Now go put your libs in <code>extras/force_load</code> and they should load when Rails starts up.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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