Notes to Myself (Rails:Entry#1b - Routes)
Published: 2021-02-21 23:41:48

![Client's Journey](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9tsz7ld4g60a25o1pm4.jpg)

Picking up where I left off, the other option for a Client is  to change something on a page.  There are 3 possible routes for such an action:  New, Edit, and Delete.

New & Edit are similar in several ways.  Each begins with a GET request that takes the Client to a Form page, and once the Form is submitted, each has a corresponding POST request that takes them back to the Controller to be executed.  This connection between the GET route & POST route is fundamental to understanding the Client's Journey, and worth highlighting.

Let's start with the New route.

     "The NEW route (GET) passes through a Form page & continues on to a CREATE route (POST)."

1) config/routes.rb
2) app/controllers/movies_controller.rb
3) app/views/movie/new.html.erb
4) app/controllers/movies_controller.rb
5) app/views/movie/1

As per our "See" requests, the Journey begins in routes.rb where the resources method sends our Client to the /movies/new path in the Controller.  

```ruby
 def new
    @movie = Movie.new
  end
```
Once there we instantiate a new Movie and set it to an instance variable. One interesting sidenote:  Unlike our respective "See" pages, this instance variable needs to exist for the Form page to be created, but it does not actually create/save a new Movie.  That happens later in the POST route when we instantiate a new Movie with the params from this Form.

The program again implicitly renders the new.html.erb page from within the appropriate views sub-folder.

```ruby
<h1>New Movie Form</h1>
<%= form_for(@movie) do |movie| %>

<p>Name: <%= movie.text_field :name %></p>

<p>Year: <%= movie.text_field :year %></p>

<p>Genre: <%= movie.text_field :genre %></p>

<p>Director: <%= movie.text_field :director %></p>

<p><%= movie.submit %></p>
<% end %>
```
Once this form is submitted, the information is stored in the params hash and sent to the Create route.

```ruby
  def create
    movie = Movie.new(accept_params)
    movie.save
    redirect_to movie_path(movie)
  end
```
In this method the params are vetted via a private method called "accept_params" that protects against malicious use, the movie is saved to the database, and we are redirected to a different route that contains a view (here the movie "show" page).

The second option for "Change" in the Client's Journey is the Edit route which is matched to the Update route.

     "The EDIT route (GET) passes through a Form page & continues on to a UPDATE route (PATCH)."

1) config/routes.rb
2) app/controllers/movies_controller.rb
3) app/views/movie/edit.html.erb
4) app/controllers/movies_controller.rb
5) app/views/movie/1

As before with our "See" routes (index & show), this sequence is virtually identical to it's sister route (new).  In this case our resources method sends our Client to the /movies/:id/edit path in the Controller. 

```ruby
  def edit
    @movie = Movie.find(params[:id])
  end
```
Once this instance variable has been set we are directed to the appropriate view, edit.html.erb.

```ruby
<h1>Edit Movie Form</h1>
<%= form_for(@movie) do |movie| %>

<p>Name: <%= movie.text_field :name %></p>

<p>Year: <%= movie.text_field :year %></p>

<p>Genre: <%= movie.text_field :genre %></p>

<p>Director: <%= movie.text_field :director %></p>

<p><%= movie.submit %></p>
<% end %>
```
After submission, it goes back to the controller as a PATCH request to the Update method...

```ruby
  def update
    movie = Movie.update(accept_params)
    redirect_to movie_path(movie)
  end
```
... and redirected back to it's show page.

The only other option left for the user is to Delete an entry, but this is a slightly unique route in that conventionally there is no "delete" page to view.  More often than not the Delete method is accessed via a link (on the Edit Form page or the Show page).  Once activated, however, it follows a very simple path.

1) config/routes.rb
2) app/controllers/movies_controller.rb
3) app/views/movies

The routes file sends a request to the controller...

```ruby
  def delete
    movie = Movie.find(params[:id])
    movie.destroy
    redirect_to movies_path
  end
```
... the movie is deleted and the Client is redirected to a page (in this case the index).