# Rails Cheatsheet

## Rails

### Creating a new project for the first time

{% hint style="info" %}
Make sure to install postgresql for your system:\
Ubuntu: <https://www.postgresql.org/download/linux/ubuntu/>\
Mac: <https://www.postgresql.org/download/macosx/> \
WSL: <https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-database>
{% endhint %}

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

**Select the compatible version of Node** using`nvm` At the time of writing, this is v14.6.0.&#x20;

{% hint style="info" %}
Node v15.11.0 has issues with Ruby v3.0.0. and Rails v6.1.3
{% endhint %}

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

{% hint style="info" %}
Rails comes with the testing framework "Mini Test" by default.  The `-T` tells Rails not to include Mini Test. &#x20;
{% endhint %}

{% hint style="info" %}
Rails will create a new Rails app with sqlite3, if you don't specify the database used.  Specify postgresql with  `-d=postgresql`
{% endhint %}

#### 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,&#x20;

```
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:&#x20;

```
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]
```

{% hint style="info" %}
"g" stands for generate
{% endhint %}

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

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

### Removing a controller

```
rails d controller [name_of_controller]
```

{% hint style="info" %}
"d" stands for destroy
{% endhint %}

This will remove your controller

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

### Setting up the postgresql database with a username and password

{% hint style="warning" %}
This is only for localhost (127.0.0.1)
{% endhint %}

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. &#x20;

```
  host: localhost
  username: pguser
  password: password
```

&#x20;Be sure to substitute your username and password for what you set!
