Skip to content

Ruby on Rails ⚓︎

Set up your dev environment⚓︎

There are several good instllation guides online, such as GoRails and Seriva's WSL2 Rails setup.

  • Update all packages: sudo apt update && sudo apt upgrade
  • Install dependencies: sudo apt install build-essential git procps curl
  • Install brew:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/admin/.profile
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
    brew install gcc
    
  • Install rbenv: brew install rbenv
  • Install Ruby:
    sudo apt-get install -y zlib1g-dev
    rbenv install 3.0.2 --verbose
    rbenv global 3.0.2
    rbenv init # Copy in ~/.profile
    
  • Install Rails:
    • Install the yarn package manager: npm install -g yarn
    • Install node: brew install node
    • Install libsqlite3-dev: apt-get install libsqlite3-dev
    • Install the rails gem: gem install rails
    • Install webpacker: rails webpacker:install

Get started⚓︎

  • To create an app (it automatically includes git init for the project): rails new MY_APP_NAME
  • To run the web server:
    cd MY_APP_NAME
    rails server
    
  • Configure git:
    git config --global color.ui true
    git config --global user.name "YOUR-NAME"
    git config --global user.email "YOUR-EMAIL"
    

Create a Rails dockerized dev environment, guide here

Random Notes⚓︎

  • Ruby on Rails follows the Model-Controller-View (MVC) architectural pattern: models are database tables, controllers are actions performed on the models, views are HTML pages rendered on the browser
  • To create a controller, rails generate controller NAME [action]
  • Views templates have .html.erb extensions
  • Django's corresponding URLs are called routes (\config\routes.rb)
  • The root route can be referenced to a specific action of a specific controller (like root CONTROLLERNAME#ACTION)
  • To check the routes in this Rails project, run rails routes or go to http://127.0.0.1:3000/rails/info/routes
  • A resource is generally anything that you want to reach with a URI and perform CRUD operations on. Simply put, it's a database table which is represented by a model, and acted on through a controller.