Naming Conventions All Versions
This draft deletes the entire topic.
Examples
-
Controller class names are pluralized. The reason is the controller controls multiple instances of object instance.
For Example:
OrdersController
would be the controller for anorders
table. Rails will then look for the class definition in a file calledorders_controller.rb
in the/app/controllers
directory.For Example:
PostsController
would be the controller for aposts
table.If the controller class name has multiple capitalized words, the table name is assumed to have underscores between these words.
For Example: If a controller is named
PendingOrdersController
then assumed file name for this controller will bepending_orders_controller.rb
. -
The model is named using the class naming convention of unbroken MixedCase and is always the singular of the table name.
For Example: If a table was named
orders
, the associated model would be namedOrder
For Example: If a table was named
posts
, the associated model would be namedPost
Rails will then look for the class definition in a file called
order.rb
in the/app/models
directory.If the model class name has multiple capitalized words, the table name is assumed to have underscores between these words.
For Example: If a model is named
BlogPost
then assumed table name will beblog_posts
. -
-
Rails files - and Ruby files in general - should be named with
lower_snake_case
filenames. E.g.app/controllers/application_controller.rb
is the file that contains the
ApplicationController
class definition. Note that whilePascalCase
is used for class and module names, the files in which they reside should still belower_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
toApplicationController
and back again.E.g. if Rails sees that the
BlogPost
class doesn't exist (hasn't been loaded yet), it'll look for a file namedblog_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 justPost
, you'll see aLoadError
:Expected [some path]/blog_post.rb to define BlogPost
.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.
-
When a controller action is rendered, Rails will attempt to find a matching layout and view based on the name of the controller.
Views and layouts are placed in the
app/views
directory.Given a request to the
PeopleController#index
action, Rails will search for:- the layout called
people
inapp/views/layouts/
(orapplication
if no match is found) - a view called
index.html.erb
inapp/views/people/
by default - if you wish to render other file called
index_new.html.erb
you have to write code for that inPeopleController#index
action likerender 'index_new'
- we can set different
layouts
for everyaction
by writingrender 'index_new', layout: 'your_layout_name'
- the layout called
-
You can get a Model class from a Controller name this way (context is Controller class):
class MyModelController < ActionController::Base # Returns corresponding model class for this controller # @return [ActiveRecord::Base] def corresponding_model_class # ... add some validation controller_name.classify.constantize end end
Topic Outline
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft