0

I try to set up helper in app/helperspath.

So, for test purpose, I did this:

#app/helpers/somehelper.rb
class SomeHelper
  def some
    return true
  end
end

I included this line in one of my controllers:

test=SomeHelper.new

That raises uninitialized constant SomeHelper error.

I tried 2 things:

In cofing/application.rb

config.autoload_paths += Dir["#{config.root}/helpers/**/"]

Didn't work.

Then I tried this:

In application_controller

 include SomeHelper

Still getting error.

How do I load helper (or any other folder) into my application load path?

2
  • 3
    It's either that you should name the file some_helper.rb or you should restart the server... Commented Nov 26, 2015 at 16:33
  • 2
    the include is not necessary and I think the helpers are autoloaded. BTW, in Rails all helpers are modules. So you could consider putting those PORO's in a seperate dir, as long as they are a child of the /app directory, they will be autoloaded. Commented Nov 26, 2015 at 16:35

1 Answer 1

1

You do not need to manually include or autoload anything, rails uses naming conventions to locate and load the file for you.

When rails encounters a new class, in your case SomeHelper, it will automatically attempt to load some_helper.rb

So all you have to do is put an underscore in the file name of your helper:

#app/helpers/some_helper.rb
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget that SomeHelper also needs to be a module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.