Rails Cheatsheet

Fri 19/03/2021

Rails

Creating a new project for the first time

Select your version of Ruby using either rvm or rbenv. I'll be using Ruby v3.0.0.

Select the compatible version of Node usingnvm At the time of writing, this is v14.6.0.

Node v15.11.0 has issues with Ruby v3.0.0. and Rails v6.1.3

Create a new project with rails new [name] -T -d=postgresql

Rails comes with the testing framework "Mini Test" by default. The -T tells Rails not to include Mini Test.

Rails will create a new Rails app with sqlite3, if you don't specify the database used. Specify postgresql with -d=postgresql

Issues with creating new projust

Change the Gemfile pg version to the lastest here: https://rubygems.org/gems/pg/versions At the time of writing, it was pg v1.2.3

Run bundle binstubs bundler

Run rails webpacker:install

Create your Database

Run db:create

Add RSpec as a testing framework

Open up your Gemfile

Under group :development, :test, add:

gem 'rspec-rails'

Then run the following command in your Terminal,

bundle install

This will install RSpec and any others you may have entered.

Setup rails to generate RSpec Test files

Run the following piece of code:

rails g rspec:install

This will make rails generate a rspec testing file when you generate a controller.

Generating a Controller

Generate a controller with:

rails g controller [name_of_controller]

"g" stands for generate

If you've set up rspec in the above step, it will also generate the rspec tests for you.

gamesfreak26@Xanthic-Laptop:~/Documents/Rails/blog_app$ rails g controller pages Running via Spring preloader in process 9637 create app/controllers/pages_controller.rb invoke erb create app/views/pages invoke rspec create spec/requests/pages_spec.rb invoke helper create app/helpers/pages_helper.rb invoke rspec create spec/helpers/pages_helper_spec.rb invoke assets invoke scss create app/assets/stylesheets/pages.scss

Removing a controller

rails d controller [name_of_controller]

"d" stands for destroy

This will remove your controller

gamesfreak26@Xanthic-Laptop:~/Documents/Rails/blog_app$ rails d controller pages Running via Spring preloader in process 9312 remove app/controllers/pages_controller.rb invoke erb remove app/views/pages invoke helper remove app/helpers/pages_helper.rb invoke assets invoke scss remove app/assets/stylesheets/pages.scss

Setting up the postgresql database with a username and password

Set up another user and substitute pguser for your postgres username

sudo -u postgres createuser -s pguser

To set a password for the user, run this command to enter postgres:

sudo -u postgres psql

While in postgres, you can change the password for pguser or the user you substituted using:

postgres=# \password pguser

Enter your password and quit postgres using:

postgres=# \q

In your config/database.yml file, enter the following under the "default: &default" section that mentions pool.

  host: localhost
  username: pguser
  password: password

Be sure to substitute your username and password for what you set!

Last updated

Was this helpful?