1

This is an evolving issue related to a previous posting I made...

I am playing around some - to try to learn how the /lib/ directory in Rails works - and how to reference variables defined in the /lib/ directory for use in a view.

I have a file called helloworld.rb and it's saved in a /lib/hellotest/ directory in Rails.

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.erb file:

module IndexHelper
  require 'helloworld'
end

I have learned that I need to include the following line of code in the /config/application.rb file:

config.autoload_paths += %W(#{Rails.root}/lib/hellotest/)

Also, I include the following code on the view index.html.erb:

<%= @howdy %>

I think I may have found something that is causing problems. I didn't want to load the entire /lib/ directory at startup so I put the file in a subdirectory called /lib/hellotest/. I've read there are some issues with how Rails interprets module/class naming conventions in the lib folder, but I can't quite figure it out. I see a good resource regarding this possible solution to my problem on William B Harding's Blog, on point 2 - but I can't quite get my arms around this solution as it pertains to my problem.

Any advice please?

What am I missing?

2scottish
  • 673
  • 3
  • 10
  • 21

1 Answers1

0

I'd suggest that unless you have a good reason to do otherwise, follow the conventional naming for modules and classes (as described in the link you provided). Rename helloworld.rb to hello_world.rb, move it into lib, and change your autoload_paths to:

 config.autoload_paths += %W(#{Rails.root}/lib/)

Finally, change require 'hello_world' to require 'hello_world' in your IndexHelper module. It should then load normally.

Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82
  • Using your suggestion - I think I'm getting closer, but the results still don't show up on the view. Is there something different I should be doing on the actual view? – 2scottish Nov 06 '12 at 22:43
  • Sorry didn't notice that part of the question. First thing, never use an extension which doesn't match the content of the file. You shouldn't be using .erb for a file with Ruby code in it, it should have the extension .rb. But actually there is no need for that file, you can include the `HelloWorld` module directly in `ApplicationController`, and then add a line `helper_method :hello` to make the `hello` method available in your views. – Chris Salzberg Nov 07 '12 at 00:00
  • For reference, here are the [docs on `helper_method`](http://apidock.com/rails/v3.2.8/AbstractController/Helpers/ClassMethods/helper_method), and a related [SO question/answer](http://stackoverflow.com/questions/4455583/rails3-how-do-i-make-a-custom-helper-available-to-controllers-and-views). – Chris Salzberg Nov 07 '12 at 00:00
  • The filename issue above was a typo in my posting, but correct in my application. I also had to change the method name to match the name of the required file: - method in the `/lib/hello_world.rb` file to `def hello_world`. - helper_method to `helper_method :hello_world`, and changed the View `<%= hello_world %>`. There must be a requirement in Rails for the method to match the filename? I also found that the `config.autoload_paths` line wasn't necessary due to the `require hello_world` line in the `ApplicationController`. Thank you again for your help! I'm starting to understand better. – 2scottish Nov 07 '12 at 23:08
  • Yes that's something I meant to mention, just didn't have time. Your files have to match the namespaces of your classes/modules, otherwise they won't get loaded. So if your class is named `HelloWorld` then the filename must be `hello_world.rb`, and if you have a sublcass `HelloWorld::SomeClass` the filename and directory also have to match, so the file should be in `hello_world/some_class.rb` (or in `hello_world.rb` itself together with the parent class). SO question on this: http://stackoverflow.com/questions/4074830/adding-lib-to-config-autoload-paths-in-rails-3-does-not-autoload-my-module – Chris Salzberg Nov 08 '12 at 00:02