Rails

Rails is a web application framework written in Ruby. In comparison to something like Node's Express, it is a much higher level instance of an application framework.

From the Ruby on Rails page[1]:

The Rails philosophy includes two major guiding principles:

  • Don't Repeat Yourself: DRY is a principle of software development which states that "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system". By not writing the same information over and over again, our code is more maintainable, more extensible, and less buggy.
  • Convention Over Configuration: Rails has opinions about the best way to do many things in a web application, and defaults to this set of conventions, rather than require that you specify minutiae through endless configuration files.

What this means is that there is quite a bit of abstraction done right out of the gate to handle the things that are most commonly implemented in websites. If you are coming from a Javascript-oriented background, this amount of abstraction and meta-programming might feel very overwhelming, but the more time you spend learning about it, you'll see that it can be used very effectively for specific types of sites.

If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.[1]

Getting Started

Prepare

  1. Install rbenv. Restart your terminal.
  2. Install ruby using rbenv. Get the latest version listed using rbenv install -l and use the latest version. e.g. rbenv install 3.4.7. Restart your terminal.
  3. Install rails using gem install rails. Restart your terminal.

Initialization

  1. Run rails new <project-name>. This initializes a git repo and prepares all the folders and everything.
  2. Commit all the new files.

Models (Database)

Database additions/modifications are done in a series of migrations. These are kept in the db/migrations folder and are just a record of the changes so it can be rebuilt from scratch.

  1. Create a new sqlite database using bin/rails db:create.
  2. Create a new table using bin/rails generate model <table> <column:type>. The types can be any listed when using bin/rails generate model --help.
  3. Create references between tables using bin/rails generate migration Add<TableA>RefTo<TableB> table_a_column:references.

Controllers

  1. Add your routes to the config/routes.rb file (see the getting started page[1] for more info here).
  2. Create a new controller using bin/rails generate controller <table> --skip-routes (use that flag only if you added routes in the before step).
  3. Update the routes as you see fit in the routes.rb file. Use the shortcut resources :<table> if you want a standard set of CRUD routes[1].

View

With the new controllers added to reference the model, you can now start getting these resources on a webpage.

References

  1. https://guides.rubyonrails.org/getting_started.html
  2. https://www.railstutorial.org/
  3. https://www.learnrails.dev/guides/setup

Last modified: 202512120615