Scooby Doo and the Ruby on Rails
Tags: rails, ruby, beginners, webdev
Published: 2020-09-30 22:10:35

Please Visit Me on GitHub: https://github.com/jlonetree
Check out My LinkedIn: https://www.linkedin.com/in/john-lonetree-52726554/

# Introduction

Scoobs and the gang want to give you, the dev, some tips and tricks to help them solve this spooky mystery, the Ruby on Rails.  Freddy Jones, ghoul trap master, has helped create this guide which goes through CRUD to develop the ultimate trap to capture The Union Pacific Specter.  In order to ultimately catch this ghoulish specter, we must understand the structure of CRUD in Rails to start building this web application/website.  The order of these actions will be set up by 3 clues with the ultimate unveiling, "destroying" this ghastly ghoul's disguise and plan.  

**C**reate - Clue #2: New, Create
**R**ead - Clue #1: Index, Show
**U**pdate - Clue #3: Edit, Update
**D**estroy - Clue #4: Destroy(Delete)

What are you waiting for, the gang is raring to capture this ghost, because after all, it's always a person in a mask.
__________________________________________________________

### Clue #1: "Read"ing a Book on Witchcraft

In CRUD, Read is important for us as a developer. It allows us to display information on the screen such as a full inventory of all items we have, showing a specific object, etc.  This is also helpful to the user while on our website, in this case The Union Pacific Specter, to become engaged/distracted when interacting with our website.  This is also the most common Scooby-Doo clue which usually includes reading a letter, or reading a specific clue because this is something we can visibly see and interact with.  This also gives us the most information about the website, or in this case the clue because we are receiving information such as descriptions, steps, titles, or anything that can be written or typed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m5y0vqs0g8xl7s0725wl.jpg)

There are two different parts that would give information to the user that hit the Read criteria in CRUD.  The Index exactly what it sounds like, it gives us all the contents of your website to show the viewer what your website may contain.  If you were to index songs that are available on your website, you could use the index page to provide a list of links to all of your song pages.  In a book, this would be similar by giving you all the chapters within a book and showing which page each chapter starts on.  This makes it easier for the user to find exactly what they want on your website.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m6tp0cm50lsn3dihequk.png)

The second part is the show page which shows a specific object, or item in better detail, giving a title, picture, page, attributes associated with it, and much more.  Using the book example again, this would be the contents of a chapter giving you details that help you understand the story, or contents of which you are reading.  The show page(s) are important for engaging and interacting with the user, and in a sense help us to communicate to them.  In this case, the Specter's using the book to communicate with the gang and you by showing this book.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6oxb1qr60d1jujtcpycs.gif)

______________________________________________________________

### Clue 2: To "Create" a Villain 

Every Scooby Doo episode had a clue relating to the creation of the villain's costume/operation.  If this specter likes creating a spooky, kooky costume, then having this user create something like a new user, or object is important for engagement and immersion to trap this horrifying host.  There are two actions for creating a new user/object, the "new", and  "create" actions.  The "new" action is a "get" method, and has a meaning like it sounds, it's getting information to provide the "create" action which is a "post" method.  The "create" action's "post" method is as it sounds as well, it posts the information in the "show" page and adds it to the "index" page which are both also "get" methods.  But wait, there's a pattern here, notice that each "get" method actually displays information except for "new".  That is because the "new" method does display information, and this information comes in the the form of a form that can be filled out by the user.  This is important information because a "get" method should always display something as part of it's action in what we call .erb files which you can find more information on those here: (https://www.rubyguides.com/2018/11/ruby-erb-haml-slim/).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g4vf9hu4yvqolkx6pzhg.png)

The "create" action works a bit differently than the previous actions such as "index", "show", and "new".  The "create" action doesn't place any information into a file for the user to see, but instead takes in information from the "new" action and does as it is titled, creating the new user/object.  Pretty neat, but that's not all, we also use this to redirect to the show page using `redirect_to` allowing the user to view their newly created object with everything the show page has to offer.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uf1ogtydb36tiw94o8c1.jpg)

______________________________________________________________

### Clue 3: An "Update" of the Villain's Scheme

Ever notice how the villains in each Scooby Doo episode leave a giant clue revealing how they updated their scheme?  This is because they needed to be more efficient, sneaky, and effective when trying to find their treacherous treasures.  Like our friend, The Union Pacific Specter, we are going to use two methods that can "update"/"edit" existing users/objects that may need tweaking for different purposes such as improvements.  "Edit" is a an action that will "get" the information we need through a .find method.  This allows us to find the certain user/object we wish to "update".  "Edit" also uses a form to to display to the user what we wish to edit such as a **user name**, or **password**.  The "edit" action then passes the information on the "update" method for it to update the information.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fleglv0km1g7m7qgzdlh.png)

The "update" action has two different methods, "patch" and "put".  These have different meanings behind them, but both will replace old values with new ones.  Think of repainting a car, with "patch" you strip the paint off the car and repaint it a new color.  With "post" this is more destructive, think of it like this, you have a ray gun, instead of stripping the paint you disintegrate the car, and build it from the ground up and paint it a different color.  Both replace the color of the car, but one creates a completely new one replacing it in a sense.  To make this short, "put" is destructive, while "patch" is not, depending on the situation either could be better.  After we create our "update" action in the controller we should redirect to the show page using `redirect_to` allowing the user to view the updated object along with other information in the show page.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0s6lv7wbofwuvzjrza0j.png)

______________________________________________________________

### Reveal: "Destroy"ing the Disguise

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/at4kcj9j21ll7auzjl5o.png)

Freddy Jones is great with traps, but Velma is the one that "destroy"s the costumed creeps with logic, and a well formulated statement after knowing just who the villain truly is.  Though our "destroy" action isn't quite like Velma's cunning explanations, it does "destroy" or as the method "delete" suggests, deletes an object and all of the values associated with it.  This makes the object disappear in our database so to say, as if it were never there to begin with.  This should also redirect us to a different page, a suggestion would be the index or home page so the user may continue using the application or website.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oirlaius9qqsw4hcmb80.jpg)

______________________________________________________________

### Conclusion

With this information on hand about how the CRUD structure operates with Ruby on Rails, you are now dangerous enough to catch The Union Pacific Specter.  If you are interested and are looking for a deeper dive on how CRUD works alongside RESTful routes & the use of Helper Methods, check out this guide: (https://dev.to/jessesbyers/how-to-rails-basic-crud-restful-routes-and-helper-methods-1h8).  Happy Hunting!  

______________________________________________________________

If you're not searching for ghouls, check out my pages:
GitHub: https://github.com/jlonetree
LinkedIn: https://www.linkedin.com/in/john-lonetree-52726554/