Getting started with Ruby on RailsRoutingActiveRecordViewsActiveRecord MigrationsRails Best PracticesNaming ConventionsActionCableActiveModelUser Authentication in RailsActiveRecord AssociationsActiveRecord ValidationsActiveRecord Query InterfaceActionMailerRails generate commandsConfigurationI18n - InternationalizationUsing GoogleMaps with RailsFile UploadsCachingActionControllerConfigurationSafe ConstantizeRails 5Authorization with CanCanMongoidGemsChange default timezoneAsset PipelineUpgrading RailsActiveRecord LockingDebuggingConfigure Angular with RailsRails loggerPrawn PDFRails APIDeploying a Rails app on HerokuActiveSupportForm HelpersActiveRecord TransactionsRSpec and Ruby on RailsDecorator patternElasticsearchReact with Rails using react-rails gemRails Cookbook - Advanced rails recipes/learnings and coding techniquesMultipurpose ActiveRecord columnsClass OrganizationShallow RoutingModel states: AASMRails 5 API AutheticationTesting Rails ApplicationsActive JobsRails frameworks over the yearsAdd Admin PanelNested form in Ruby on RailsFactory GirlImport whole CSV files from specific folderTools for Ruby on Rails code optimization and cleanupActiveJobActive Model SerializersRails Engine - Modular RailsSingle Table InheritanceActiveRecord TransactionsTurbolinksFriendly IDSecurely storing authentication keysAuthenticate Api using DeviseIntegrating React.js with Rails Using HyperloopChange a default Rails application enviornmentReserved WordsRails -EnginesAdding an Amazon RDS to your rails applicationPayment feature in railsRails on docker

Naming Conventions

Other topics

Controllers

Controller class names are pluralized. The reason is the controller controls multiple instances of object instance.

For Example: OrdersController would be the controller for an orders table. Rails will then look for the class definition in a file called orders_controller.rb in the /app/controllers directory.

For Example: PostsController would be the controller for a posts 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 be pending_orders_controller.rb.

Models

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 named Order

For Example: If a table was named posts, the associated model would be named Post

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 be blog_posts.

Views and Layouts

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 in app/views/layouts/ (or application if no match is found)
  • a view called index.html.erb in app/views/people/ by default
  • if you wish to render other file called index_new.html.erb you have to write code for that in PeopleController#index action like render 'index_new'
  • we can set different layouts for every action by writing render 'index_new', layout: 'your_layout_name'

Filenames and autoloading

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 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.

E.g. 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.

Models class from Controller name

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

Contributors

Topic Id: 1493

Example Ids: 4860,4861,5859,5875,9626

This site is not affiliated with any of the contributors.