revolver
revolver

Reputation: 2405

Requiring a file in ROR

I have a controller which wish to include a file in another folder, how do I do that? I am using Ruby 1.9.2 and Rails 3.2.6. Thanks

Controller file is in

/project/app/controllers/examples_controller.rb

and the required file is in

/project/example_folder/example.rb

Upvotes: 0

Views: 48

Answers (4)

wizztjh
wizztjh

Reputation: 7041

according to here

more info here Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?

requie_relative '../../example_folder/example.rb'

Upvotes: 0

Gawyn
Gawyn

Reputation: 1156

It would be something like this:

require "#{Rails.root}/example_folder/example.rb"

just before the declaration of your controller.

Upvotes: 0

Erez Rabih
Erez Rabih

Reputation: 15788

require File.join(Rails.root, "example_folder", "example")

assuming /projects/ is your rails application root folder

Upvotes: 2

Shamith c
Shamith c

Reputation: 3739

You can auto load custom directories with classes and modules you want to be.

So you can Edit config/application.rb

# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/example_folder)

Upvotes: 0

Related Questions