Helper Methods in Ruby
Published: 2023-10-13 20:45:48

With the introduction of scaffolding and helper methods, there's a lot of new information to keep track of and a lot of the information that I learned previously will no longer be used regularly. I'm writing this blog post to organize some of the new material and for quick reference for later use.

_This post is a work in progress and I will be gradually adding to it over the next few days as I revisit the last few lessons._

**Setting up your project: Scaffolding**

When you open up a new, blank Rails project, the first step is to set up your MVC. Rails has an easy built-in generator that doesn't require a gem in order to run: `scaffold`.

To use the scaffold generator, type the following into a bash prompt:

```
rails generate scaffold <table_name_singular> <column_name>:<data_type> <column_name>:<data_type> <column_name>:<data_type>
```

Run this and then run `rake db:migrate`.

What's included with this?

- the model.rb for the database table
- RESTful starter routes (CRUD) using conventional naming patterns
- corresponding templates

Once these are set up, they must be edited for your own needs! It's just starter code.

**New Syntax**

As we move into this new way of doing things, we are using updated and more professional/concise syntax in our projects.

What does this look like? We'll use the database table name "movies" like we did in lecture.

In routes.rb:

`get "/movies", controller: "movies", action: "create"`

becomes:

`post "/movies" => "movies#create"`

Additionally, the root route has its own syntax:

`root "movies#index`

Further, we will assign method names to each route in the routes.rb file and refer to the routes by their method names in any other file in the project instead of by the url. This helps a lot if you ever have to change the name of the route - rather than going through your whole project and finding any reference to the route, you can just change the method name in routes.rb.

`post "/movies" => "movies#create", as: :movies`

The convention for each route is as follows:
Create: 
/movies/new = :new_movie
post /movies = :movies

Read:
/movies - you don't have to put anything.
/movies/:id = :movie

Update:
patch - you don't have to put anything.
/movies/:id/edit = :edit_movie

Delete:
You don't have to put anything for this route.

**I don't really get why you don't have to put the helper method for all of them so I have to figure that out.**

**link_to**

The `link_to` helper method is generates links for you as opposed to having to write them yourself. It enables you to use the route method names you previously set up and can actually find these by itself if you're using conventional naming patterns.

**form_with**

The `form_with` helper method can replace the `<form>` HTML tag and has a few benefits including automatically generating the authenticity token we need to prevent malicious attacks (cross-site request forgery). Forms with a path other than get cannot be submitted without this token. It takes a hash as an argument that contains the path. You can also include the method if it's something like patch so you don't have to include this as a hidden input anymore. We are also including `data: {turbo: false}` because turbo is automatically on in the version of Ruby we're using.

`form_with` is a code block so you have to then end the block at the end of the form. 

We use a label tag like so:

`<%= label_tag :description, "Description" %>`

The first argument to the `label_tag` is the `for=` attribute from our old label tags, which connects the label to the input field via the input field's ID. The second one is the content if that's something different - what the label actually says.

There are different helper methods for each type of input such as a text area, checkbox, etc.  For these, the first argument is the name & id (if they're the same) and the second populates the value attribute. You can also add a hash for additional attributes like rows in a text area or the id if it's not the same as the name.