Ruby on Rails - Filenames and autoloading in ruby on rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails



Filenames and autoloading

Rails files - and Ruby files in general - should be named with lower_snake_case filenames.

Example

app/controllers/application_controller.rb
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

the file that contains the ApplicationController class definition. Note that while PascalCase is used for class and module names, the files in which they reside should still be lower_snake_case.
Consistent naming is important since Rails makes use of auto-loading files as needed, and uses "inflection" to transform between different naming styles, such as transforming application_controller to ApplicationController and back again.

Example

if Rails sees that the BlogPost class doesn't exist (hasn't been loaded yet), it'll look for a file named blog_post.rb and attempt to load that file.

It is therefore also important to name files for what they contain, since the autoloader expects file names to match content. If, for instance, the blog_post.rb instead contains a class named just Post, you'll see a LoadError: Expected [some path]/blog_post.rb to define BlogPost.
If you add a dir under app/something/ (e.g. /models/products/), and

  • want to namespace modules and classes inside new dir then you don't need to do anything and it'll be loaded itself. For example, in app/models/products/ you would need to wrap your class inmodule Products`.
  • don't want to namespace modules and classes inside my new dir then you have to add config.autoload_paths += %W( #{config.root}/app/models/products ) to your application.rb to autoload.

One more thing to pay attention to (especially if English is not your first language) is the fact that Rails accounts for irregular plural nouns in English. So if you have model named "Foot" the corresponding controller needs to be called "FeetController" rather than "FootsController" if you want rails "magic" routing (and many more such features) to work.


This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Filenames and autoloading in ruby on rails