How to add a new class folder in rails app?

I have 2 questions in the context of a rails application:

I have several classes that are not "mode" but require on my system, so I want to separate them

1) How to add the "class" folder to app /? (if I create it and put the classes in, they are not included)

2) how can I put the "model" folder in the "app / class" folder (same here, the model is not included if I move it)

THX.

+3


source to share


2 answers


It is not clear what you are asking.

But if you want to autoload additional directories, you can do so by putting something like this in config/application.rb

config.autoload_paths << Rails.root.join('app/class')

      

But please don't name your directory class

, use something descriptive instead.

By conditional code that doesn't fit inside models, controllers, views, helpers or problems and is placed in a directory lib

at the root of the project.




Edit:

You can load subdirectories using glob:

config.autoload_paths << Rails.root.join('app/classes/**/')

      

+5


source


For some time now, Rails will automatically load all paths in / app as mentioned here

You might have a problem using the app / class directory, because class is a reserved word and class is a Ruby class.

Your example has a problem:



Example: "app / classes / effects / attribute.rb" with "class Effect :: Attribute"

Note that in the file path "effects" has an "s" at the end, whereas your module name is not "Effect :: Atttribute". They must match. Either they both end in "s" or they don't, and when they are Rails compliant, autoloading should work.

You should remove any other suggestions for adding to config.autoload_paths.

+2


source







All Articles