0

I am very new to Rails, and am trying 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 the /lib/ 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.rb file:

module IndexHelper
  require 'helloworld'
end

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

<%= @howdy %>

What am I missing?

2scottish
  • 673
  • 3
  • 10
  • 21

3 Answers3

1

You should any of these lines to config/application.rb file.

module [App name]
  class Application < Rails::Application
    # Dir.glob("./lib/*.rb").each { |file| require file } 
    # config.autoload_paths += %W(#{Rails.root}/lib)
  end
end

Uncomment any of commented lines. Both of them do same work.

Dir.glob finds all .rb files in app and require each file in rails app.

Also config.autoload_paths also load all files in lib folder.

Akshay Vishnoi
  • 1,252
  • 12
  • 15
1

You need to call Helloworld::hello in order for it to create your instance variable.

maybe you could put it in a before_filter in your controller

require 'helloworld'

class FooController < Application::Controller

  before_filter :setup_hello , [:only=>:create, :edit ]
  def create
     # whatever
  end
  def edit
     #whatever
  end
  def setup_hello
    HelloWorld::hello
  end
end

So now, every time you either your edit or create action, 'setup_hello' is executed, which calls the hello method in your module, and sets the @hello instance variable.

RadBrad
  • 7,234
  • 2
  • 24
  • 17
  • I must be missing something... When I implement this in my controller, I get the following error: undefined method `hello' for Helloworld:Module – 2scottish Nov 05 '12 at 21:43
  • Hmmm... I must still be doing something wrong, as I get the same error message. The error message points to the line in the controller that references `Helloworld::hello`. Also, am I correct to assume that in the View, I can reference this code by using: `<%= setup_hello %>` ? – 2scottish Nov 05 '12 at 22:52
  • Fixed ANOTHER typo ('HelloWorld') vs ('Helloworld') and added more context and explanation. – RadBrad Nov 05 '12 at 23:58
  • NoMethodError in ReportsController#index undefined method 'hello' for HelloWorld:Module Rails.root: C:/rails_projects/helloworld app/controllers/reports_controller.rb:8:in `setup_hello' – 2scottish Nov 06 '12 at 19:35
  • Thanks Brad. Combined with your help and my other posting on this subject (http://stackoverflow.com/questions/13260387/using-lib-subdirectories-in-rails-for-view-content), I was able to figure this out! – 2scottish Nov 07 '12 at 23:24
0

You have to add the lib folder to the auto-load path in config/application.rb

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/lib)
Luke
  • 3,381
  • 1
  • 20
  • 20