Rails Routes - CRUD & RESTful
Published: 2020-04-09 04:49:43

CRUD

C = Create
R = Read
U = Update
D = Delete

CRUD are the four ways we can interact with data. These are the basic functions that all models should have. 

REST = Representation State Transfer
REST is an architectural pattern for defining routes. It is a way of connecting the HTTP verbs (GET, POST, PATCH/PUT, DELETE) and the CRUD actions. RESTful routing allow developers to keep things simple and consistent for other developers or users to use.

Imagine we had the model / class: Recipe 

## Create 
Need to be able to create a new class object and save the object to the database.

Create the routes - two parts.
 
#### First - GET the HTML form to create new object, in this case, recipe.
```ruby
# In the routes file
get '/recipes/new', to: 'recipes#new', as: 'new_recipe'

- get = HTTP verb
- '/recipes/new' = route
- recipes = controller name
- new = action that will be in the controller file

* as: is an option that you can use to rename your route if needed
```
This is what the controller action would look like in the controller file:
```ruby
# In the recipes_controller.rb file

class RecipesController < ApplicationController

def new
end

end
```

#### Second - POST the new recipe

```ruby
post '/recipes', to: 'recipes#create'

- post = HTTP verb
- '/recipes' = route
- recipes = controller name
- create = action
```
```ruby
# In the recipes_controller.rb file

class RecipesController < ApplicationController

def new
end

def create
end

end
```



## Read
There are two ways we can read / get data from the database. We can get **all** of the objects, recipes, of a class or a **specific** object of a class.

```ruby
get '/recipes', to: 'recipes#index'

- index = action
```
Index page will render the index view page that will display **all** the objects of a class.

```ruby
get '/recipes/:id', to: 'recipes#show'

- :id = id of the record / object that was 
automatically assigned when record was created in 
the database
```
The id number is needed in order to find the object and then render the show page (show view) of that **specific** object.

```ruby
# In the recipes_controller.rb file

class RecipesController < ApplicationController

def new
end

def index
end

def show
end

end
```



## Update
Also has two parts: 
#### First - Need route and controller action to render the edit form (edit view).

```ruby
get '/recipes/:id', to: 'recipes#edit'
```
Note: Need to pass in id number again in order to find the **specific** object we want to edit.

#### Second - Need route and controller action to update the object
```ruby
patch/put '/recipes/:id', to: 'recipes#update'

- patch & put = HTTP verbs. Use patch when you
 want to make a small change to the object. Use 
put when you want to replace everything.
```
```ruby
# In the recipes_controller.rb file

class RecipesController < ApplicationController

def new
end

def create
end

def index
end

def show
end

def edit
end

def update
end

end
```


## Delete
Use to delete a record / object from database.

```ruby
delete '/recipes/:id', to: 'recipes#destroy'
```

```ruby
# In the recipes_controller.rb file

class RecipesController < ApplicationController

def new
end

def create
end

def index
end

def show
end

def edit
end

def update
end

def destroy
end

end
```
A shortcut to get all seven of these routes in rails is to write:

```ruby
resources :recipes
```

You can also limit down to specific routes by using only. Just list the routes you need.

```ruby
resources :recipes, only: [:new, :create, :show]
```