Rails conventions

written by Dariush 5 months ago

Naming Classes and Files

ClassNames are written in UpperCase (where each word start with a capital letter). That is for the class names.
Now file_names are written in small letters with underscore, as you see here like file_names

Views

The folder in the views folder that store the template is viewed after the controller name A template has a one-to-one relationship with the action within a the controller that is mapped to

Layouts

Layouts control the global layout of the application, the stuff that does not change from page to page. partials are pieces of code that act like the good old include function in php.

Controllers

Instance variables in controllers are defined like @variable. These guys can pass their values down to the corresponding view. When creating a controller with the generate command, you need only to specify the controller’s name. ie. script/generate controller Dariush

what I mean is that you don’t need to write DariushController, otherwise you end up with DariushControllerController.

Keep application logic in the controller according to Patrick Lenz’s book “Build your own ruby on rails application”

Helpers

helper files are named after the associated controller. controller name = controllername its helper = controllername_helper

and they reside in the /app/helpers

Remember, Helpers associated with a controller such as the one above, are only available to the views of that particular controller, with one exception. The group of helpers defined in the /app/helpers/application_helper.rb file are available to any view in the entire application.

Migration

To force a table drop in case the table already exists, here is what we can do:

 
def self.up
   create_table :stories, :force => true do |t|
     t.column  :name, :sring
     t.column  :email, :string
   end
end

Note: Migration will create the id automatically. So don’t stick an id in there.

Post a comment

Options:

Size

Colors