I have a Rails engine with the latest rails and ruby.
I have a controller called cms, with a action called update. I use this update action to update different tables. For example I have got a table called setting. This technique works fine in a normal Rails app, but in my Rails Engine it throws this error:
NameError (uninitialized constant Setting):
I've got a model called Setting, why is givin me an error ?
File naming is important for autoloading to work. Naming convention is the same in both apps and engines. In fact, an application is an engine.
So, my_rails_app/app/models/cms/setting.rb is equivalent to my_engine/app/models/cms/setting.rb
If you still have troubles, try accessing constant with explicit namespace Cms::Setting.
You can dynamically get constant from an appropriate namespace by doing
Cms.const_get(table.capitalize)
However, be careful with this approach since a hacker can send you anything and hence access any constant.
Related
I did a Rails extension as suggested by
extending rails question using Harish Shetty answer.
I like it so much I decided to use it in my Rails Engine. Didn't quite work.
To make it work i place the file active_record_extensions in the Lib/[ENGINE] directory and added the Require active_record_extension to my engine.rb file.
This works, but I wonder if there's is a more proper way to do it.
Also. I ran into an Issue, (don't know if i'm allowed to piggyback a second question).
I have a Model called Translation. When i use the extended class method with that model i invoke an error 'Circular dependency detected' with
raise "Circular dependency detected while autoloading constant #{qualified_name}"
I'm guessing that using the model extension exposed the name to something that doesn't like the fact that I used Translation. (doesn't happen with the other models I have). I get around it by not using the extend class method with that model.
There are obviously some classes that are automatically loaded when I start "irb" using "rails c". So how do I ensure my own classes are loaded? Am I expected to use "require"?
I get "NameError: uninitialized constant" whenever I try to use my classes.
If the classes are included as part of the Rails app though the Gem bundle, or you're require-ing the classes elsewhere in the app, then they should be loaded along with the Rails app into the console.
If they're completely indepent of Rails (e.g. the Rails app doesn't load these classes), then you'll have to require them explicitly.
As far as I understand, in Rails you don't have to require most of the files that you use, and that most of these files are auto-magically required or included in your code as you reference appropriate classes.
If I understand If I do the following in some arbitrary file, or via running a script with rails runner myscript.rb
myscript.rb
User.delete_all
#set up a default user
User.create(name: "default", password: "default")
This file automatically sees a class it doesnt recognize, User, and understands via its naming convention that the class must be defined in /app/models/user.rb then this code is somehow made available via require or something similar.
My question is: How does rails implement this feature? This is something I'd very much like to understand.
The answer depends a lot on the environment. In production, everything is loaded at boot time and all the classes are cached. In development, the classes are found with const_missing and reloaded when they change. Take a look here at the Rails initialization process.
Check method autoload, whose planned deprecation has been halted for now, and also const_missing, to name just two. Another available mechanism would be eg. to rescue NameError exceptions for uninitialized constants...
I'm running Rails 2.3.4. When I create a new rails project, the public/index.html file has a link named "About your application's environment" that points to "rails/info/properties". In dev mode, it gives a summary of the runtime environment. However, in production mode, it gives a 404 page cannot be found.
Could someone point me in the direction of how and where the "rails/info/properties" route is configured? I'd just like to understand how it's set up.
The link fires off an AJAX request to rails/info/properties. The properties action is defined in the Rails::InfoController which lives in /rails/railties/builtin/rails_info/rails/info_controller.rb.
The route doesn't need to be explicitly defined because it conforms to the Rails default route of :controller/:action/:id (although there is no ID in this case and the controller lives within a Rails namespace.)
It's configured within Rails itself (when in development mode). You can probably track it down if you look through the Rails initialization code.
I am writing a rails app, which has some modules and methods loaded via the lib directory, we have also added some methods in restful authentication module, now everytime when i run using mongrel server on production i dont get this error, although because model classes and controller classes are cached, but in development i do get this error, after the first time, so i start my server on development and run once i get the correct behavior, but when i refresh the page i get this nil.include error, while it displays that the each method is getting a nil to iterate, also i have done some homework, as i added the puts method, for all methods for that instance while iterating, i dont get any method definations for attributes of the table, like ID, name, title. i am unsure about this,
I am using Rails 2.2.2 and ruby 1.8.7.
Server is mongrel & webrick.
This situation happened to me, where I used to get the error only after the refresh (i.e., second call to the server).
It may be that you have defined a module in your /lib directory which does not matches the rails convention of file-naming. And you are using the require "filename.rb" statement to get the module.
Try using require_dependency "filename.rb" instead.
Let me know if problem still exists, we may look deeper.
You should look into the log/development.log or log/production.log files (depending on the environment you are investigating). The first line of the stacktrace shows the source of the error. Try to find the error there or post the relevant line (this and some above).