Rails, Ruby

Ruby on Rails 101

First thing that you should know about Rails is: it runs on convention.

Since Rails is a well established system, it’s easy for newcomers to miss all the implied links between its different parts. For example, the name mappings for one, e.g.:

Area Map From Map To
Controller actions index method in controllers/articles_controller.rb views/articles/index.html.erb
Routes variable “articles_path” CRUD “articles” related routes
Active Record column names model attributes

..And much much more. When you can’t get something running, there’s a high chance that you are not naming something according to convention, and therefore Rails cannot find the files or classes where it thinks they are.

Also pay attention to the Plurality and Singularity when it comes to naming, e.g.,

Area Singular Plural
Resource in routes articles
Route articles_path
Generator Controller bin/rails generate controller Comments
Model Article
Article.first
Table/Schema articles

 

Q: What is a helper? What does “articles_path” mean?

A: xx_path tells Rails to point the form to the URI pattern associated with the “xx” prefix.

 

Q: One quick way to pass values from controller to its view?

A: with instance variables. Anything in controller.rb defined as @var you can access in view with,

<%= @article %>

 

Q: What is Active Record?

A: It’s an “active” representation of a database model. A handle on the real thing. Therefore, we get to do all kinds of database operations without actually needing to touch SQL queries. For example, search, sort, define associations..

 

Q: What is the default environment the commands run in when environment is not specified?

Development. These two are equivalent:

bin/rails db:migrate RAILS_ENV=development
bin/rails db:migrate
Standard

Leave a Reply

Your email address will not be published. Required fields are marked *