Skip to content

Instantly share code, notes, and snippets.

@ParthBarot-BoTreeConsulting
Last active October 13, 2015 19:38
Show Gist options
  • Save ParthBarot-BoTreeConsulting/22b5f5432ea4a9163edd to your computer and use it in GitHub Desktop.
Save ParthBarot-BoTreeConsulting/22b5f5432ea4a9163edd to your computer and use it in GitHub Desktop.
Rails Cheatsheet - basic rules to remember and use in every code you write

Some basic rules to follow, for diff. rails components. Please add comment if you want to add anything.

##Controllers

  1. Extract common code in controller action, which loads data from DB using finder methods with params as arguments.
  2. Same for any service object loading.
  3. Above two cases, if being used in more than one controller, then define a concern using included do before_action :finder_method end and include that in both.
  4. Do not write separate controller for API, instead use respond_to with diff. data format.
  5. Single instance variable per action.
  6. Extract any code which is not rails related, as a separate ruby class which can be used as a reusable utility. Like, CsvImporter, PaypalUtil, GoogleAnalyticsUtil etc. Do not extract all business logic as service/util, that is not necessary.
  7. Extract more controllers, based on the workings of your actions. Do not put all actions in one controller.

##Models

  1. define concerns/modules for diff. DB columns to keep them separated using included do has_one :model_name end block. Same as point 3 of controllers.
  2. Use ActiveRecord queries properly, do not use it in loops. Instead we can use SQL queries when needed to optimize the execution time.
  3. Understand when model gives you AREL object, and when ruby object, based on what AR methods you have called.
  4. Understand aggregate functions, ordering/sorting, inner join, left/right outer join etc. This will be very helpful.

##Views

  1. Never use %img, only use = image_tag
  2. Use helper classes, define methods, when there is some ruby logic needed in templates.
  3. Always use HAML if possible, that is easy to write.
  4. Use partials properly when needed.
  5. Use diff. views for diff. types of format - .js.haml, .html.haml etc.
  6. js.erb does not work.

##Javascripts

  1. Controller/action specific Javascript, which should load only on that controller/action. Using Object literal pattern, do not add document.ready at any random place.
  2. Never use asset images using static URLs in JS, instead make CSS classes and use them. js.erb does not work!
  3. Always use manifest files for point 1.

##CSS/SCSS

  1. Use SCSS if it has urls for assets, use helpers like font-url, asset-url for them.

##Routes

  1. Always define routes using specific method - get/post/put/patch/delete etc.
  2. Always use resources or module when needed, do not use match unless it is required for multiple methods like get n put both.

##Tests/specs

  1. Define tests for each model, controller and service/util classes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment