rails / rails

Ruby on Rails
https://rubyonrails.org
MIT License
55.96k stars 21.64k forks source link

Problems loading modules and classes #20169

Closed ciaoben closed 9 years ago

ciaoben commented 9 years ago

I am trying to create custom exceptions in rails, but I 've problem with my designed solution.

Here what I've done so far:

-Create in the app/ folder a folder named errors/ with a file exceptions.rb in it.

app/errors/exceptions.rb:

module Exceptions
    class AppError < StandardError; end
end

raise Exceptions::AppError.new("User is not authorized")

But when I call the controller's action, here is what I get:

NameError (uninitialized constant Exceptions::AppError

Did you mean? TypeError
              KeyError
              IOError
              EOFError

Did you mean? TypeError
              KeyError
              IOError
              EOFError
):

I think I don't have fully understood how to create new directories and files, and use them.

I've read that everything created in the app dir, is eager loaded, so I can't understand where is the problem.

meinac commented 9 years ago

In your application.rb file extend your autoload paths like below;

config.autoload_paths << Rails.root.join('app', 'errors')

And please use stackoverflow or similar platforms for asking 'how to questions'.

ciaoben commented 9 years ago

Why it has been closed?

The solution provided doesn't work!

And this is not an "how to". Rails doesn't load automatically all the new folders in app??? Why this isn't the case-

meinac commented 9 years ago

@ciaoben Also you can use spring stop then rails s to refresh your application. By this way you don't need to explicitly extend autoload_paths. Try this and tell me the result please.

ciaoben commented 9 years ago

@meinac yes, it seems to work. Thanks. I am using the console, so I've done spring stop and rails c and everything worked fine, without

config.autoload_paths << Rails.root.join('app', 'errors')

Can you give me a brief explanation?

rafaelfranca commented 9 years ago

spring eagerload you application and keep it running. When you add a new folder to app, rails automatically add it to autoload paths but since you spring process already started it doesn't know that a new folder was added, so you have to restart the spring process to get the new path on the autoload paths

ciaoben commented 9 years ago

Thanks, it is all clear now!