uninitialized constant GamesController::GamesAccounts - ruby-on-rails

created a games_account.rb file in the library folder. The following is the structure
module GamesAccounts
class GamesAccountsClient
.
.
.
.
.
end
end
trying to do GamesAccounts::GamesAccountsClient.new in the controller gives me the error
uninitialized constant GamesController::GamesAccounts
I have even added
config.autoload_paths += %W(#{config.root}/lib) in the applications.rb
Am i doing anything wrong here?

I'm not sure about this, but I think you might need to put it in lib/games_accounts/games_accounts_client.rb instead of what you have now, which I presume is lib/games_account.rb. The idea is it should be lib/<module name>/<class name>.rb.

The problem is in your file, name it games_accounts.rb instead of games_account.rb and it should work (because it will match the module name).
If you plan to put many classes within this module, create a directory named games_accounts, and add the class there with the mapping of each file to each class, and put it on your application.rb file, like
config.autoload_paths += %W(#{config.root}/lib/games_accounts)

I have a other thought, If your file is something which is be helping the models than try having it in the form of concerns folder and adding your file there. Since Rails 4 onwards all these support activities will be taken by concerns it's good to adopt right away. Have a read at the blog post by DHH also:
http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns

Related

Not able to include Module in a Class in Rails Application

I have a class(Server) that include module(ServerConstants)
class Server
include ServerConstants
Both files reside inside lib/server folder
I get an error : Unable to autoload constant Server::ServerConstants
When I use require explicitly it works fine.
I read about Rails Autoloading feature and wonder why it is not picking it up . My application.rb files seems to include autoload paths correctly.
config.autoload_paths += Dir["#{config.root}/lib/**/"]
config.autoload_paths += Dir["#{config.root}/app/models/**/"]
What I am missing ?
We are using Rails 4.2.8
Finally I found an answer by Hit and Trial , although I am not sure why it worked.
Instead of defining module like
module ServerConstants
I used
module Server::ServerConstants
and it worked !
Another Finding :
This problem only occurs when you have same name of folder and class including module. In my case server folder contains both Server class and ServerConstants module. When I refactored name of Server to Server_X , the problem faded away.
Include statement directly looks for modules defined without scope or conflicts with names. But when you place it inside lib/server/ directory, it can be access by telling include statement, which directory to look in to find ServerConstants module. That is why if you use include Server::ServerConstants, it works as now you have mentioned which folder to look in and now scope of the module that you want to access is now mentioned.

How add new folder with class in rails app?

I have 2 questions in rails app context:
I have some classes which aren't "modele", but require in my sytem, so I want separe them
1) How can I add "class' folder in app/? (if I create it and put classes, their are no included)
2) how can I put folder "model" in "app/class" folder (same thing here, the model are not included if I move it)
thx.
It´s kind of unclear what you are asking.
But if you want to autoload additional directories you can do it by placing something like this in config/application.rb
config.autoload_paths << Rails.root.join('app/class')
But please don´t call your directory class, use something descriptive instead.
By convention code that does not fit inside models, controllers, views, helpers or concerns and placed in the lib directory at the project root.
Edit:
You can load subdirectories by using a glob:
config.autoload_paths << Rails.root.join('app/classes/**/')
For quite some time Rails has autoloaded all paths under /app, as mentioned here
You may have run into a problem when using a "app/class" directory since "class" is a reserved word and "Class" is a class in Ruby.
There is a problem with your example:
exemple: "app/classes/effects/attribute.rb" with "class Effect::Attribute"
Notice that in the file path "effects" has an "s" at the end, whereas your module name does not "Effect::Atttribute". Those should match. Either both end with "s" or not, and when they do match Rails autoloading should work.
You should remove any of the other suggestions about appending to config.autoload_paths.

Rails Module/Folder Naming Convention

I'm having a problem with a module name and the folder structure.
I have a model defined as
module API
module RESTv2
class User
end
end
end
The folder structure looks like
models/api/restv2/user.rb
When trying to access the class, I get an uninitialized constant error. However, if I change the module name to REST and the folder to /rest, I don't get the error.
I assume the problem has to do with the naming of the folder, and I've tried all different combos of /rest_v_2, /rest_v2, /restv_2, etc.
Any suggestions?
Rails uses the 'underscore' method on a module or class name to try and figure out what file to load when it comes across a constant it doesn't know yet. When you run your module through this method, it doesn't seem to give the most intuitive result:
"RESTv2".underscore
# => "res_tv2"
I'm not sure why underscore makes this choice, but I bet renaming your module dir to the above would fix your issue (though I think I'd prefer just renaming it to "RestV2 or RESTV2 so the directory name is sane).
You'll need to configure Rails to autoload in the subdirectories of the app/model directory. Put this in your config/application.rb:
config.autoload_paths += Dir["#{config.root}/app/models/**/"]
Then you should be able to autoload those files.
Also, your likely filename will have to be app/model/api/res_tv2/user.rb, as Rails uses String.underscore to determine the filename. I'd just call it API::V2::User to avoid headaches, unless you have more than one type of API.

Using /lib/ Subdirectories in Rails - for View Content

This is an evolving issue related to a previous posting I made...
I am playing around some - to try 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 a /lib/hellotest/ 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.erb file:
module IndexHelper
require 'helloworld'
end
I have learned that I need to include the following line of code in the /config/application.rb file:
config.autoload_paths += %W(#{Rails.root}/lib/hellotest/)
Also, I include the following code on the view index.html.erb:
<%= #howdy %>
I think I may have found something that is causing problems. I didn't want to load the entire /lib/ directory at startup so I put the file in a subdirectory called /lib/hellotest/. I've read there are some issues with how Rails interprets module/class naming conventions in the lib folder, but I can't quite figure it out. I see a good resource regarding this possible solution to my problem on William B Harding's Blog, on point 2 - but I can't quite get my arms around this solution as it pertains to my problem.
Any advice please?
What am I missing?
I'd suggest that unless you have a good reason to do otherwise, follow the conventional naming for modules and classes (as described in the link you provided). Rename helloworld.rb to hello_world.rb, move it into lib, and change your autoload_paths to:
config.autoload_paths += %W(#{Rails.root}/lib/)
Finally, change require 'hello_world' to require 'hello_world' in your IndexHelper module. It should then load normally.

Ruby Rails Lib Folder Naming Convention

I seem to be having trouble with the naming conventions of the Lib Folder in Rails, and the error messages provided to me do not help. [For example, I have received a message saying that XXX::YYY::TextBox is expected to be defined xxx/yyy/text_box.rb, even though it clearly was defined there.] I think I'm getting the convention wrong.
Let's say I am working on YourModule::MyModule::MyClass. I clearly get that this file should be located in
lib/your_module/my_module/my_class.rb
But what should the actual file here look like? Which one of these (if either) are correct?
#your_module/my_module/my_class.rb
module YourModule
module MyModule
class MyClass
...
end
end
end
Or
#your_module/my_module/my_class.rb
class MyClass
...
end
In other words, do I need to nest the class inside of the module structure or not?
The lib folder has few conventions, as it is not autoloaded. So, how you organize the files is up to you, but you do have to name the classes correctly. Your first example is correct.
To get the files included you need to specify you want them in your application.rb file, see this example: Best way to load module/class from lib folder in Rails 3?
I would recommend making a folder just called lib/modules, since you probably won't have very many. Name the file my_class.rb. Then in application.rb you need:
config.autoload_paths += %W(#{config.root}/lib/modules)
That should take care of your issue.

Resources