Rails Project: Spells Library Application
Published: 2021-02-07 22:34:15

I am finally done with my first rails project! I decided to start on it early so I could apply what I was learning in class to my code in real time. I learned some things about myself as a programer during this project. I have always thought of myself as an artsy person and expected to spend extra hours perfecting the design or adding a cool carousel, which I did, but I was more excited when I could apply a new function and see it work. Who knows where my career will take me but at this point in time I feel a strong connection when working on the backend. Alright let's show you how I made this app.

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

I should explain the purpose first. This is an application that a user can log into and store books and spells within those books. When planning I needed to figure out the overall function and decided that a user would have_many books and a book would have_many spells. These are Active Record relationships. On the other side of that, a spell would belong_to a book and a book would belong_to a user. Wait... there's more, a user has_many spells through books and spells has_many users through books, which would make books my joins table. I definitely needed to review this part a bunch before I settled on those relationships but it was what made the most sense to me and my apps functions. By using Active Record associations, we can tell Rails that there is a connection between these two models. Every time we tell Rails that one model belongs_to another, we having it set up and maintain a Primary Key and Foreign Key. So in my situation, every time a spell in made within a book, they are assigned a set of keys letting the program know they have a relationship.

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

Before I was able to code all of this in I needed to create the shell of my project. I made a directory to store my project and then ran "rails new witches-spells" in the terminal to get the initial set up. Once this was loaded up I quickly connected to github so I could push all of my setup changes to it. There were 91 changes for my initial set up. I worked really hard to make meaningful consistent pushes when changes were made. Pushing these first 91 as the initial set up was helpful because if I got lost later in my code I could go to github and check what was made for me and what changes I did after. Next task was to run "rails g resource User name:string". This set up my first migration file for the user table along with its first column, name. It also set up, a user model file that inherits from Application Record, a user controller that inherits from Application Controller, a user view directory, a user view helper file, and a full resources call for users in the routes.rb file. It actually created more files but I did not use them for this project. I did go into my migration file and add more columns like an email and password. I did this same process for my other tables, books and spells. When I was done with all my tables, I added my Active Record relationships to the models, then I ran "rails db:migrate" which migrated the database and gave me my schema. Without this I could not make new objects to save.

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

Now that the structure was set up I needed to give my controllers some function. I started by adding methods for index, show, new, create, edit, update, and destroy. These are the most basic methods I needed to have a well functioning application. Eventually I did decide to leave some out. For example, my user controller does not have a index or destroy method because I did not was a page listing all of the users or an option for people to destroy their account. Looking back I might of left them in and modified my routes to not display them but I had other things to work on. Which brings me to my nested params/forms/routes. 

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

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

Within my application there is the ability to make spells when you are creating a new book. You can only make the book or you can make the book and give it up to three spells to start off with. Same goes for making spells. You may chose a book to which the spell belongs or you can create the new book along with it. The user sees and interacts with this through my nested forms that are created in the views. In order to get those working, I needed to nest the params within each other. I also needed to put a accepts_nested_attributes_for in my book model so everything would run smoothly. The spell views have a partial so that I can add in the book form easily and keep the code clean. In my book views, I only call on the form once so it is written out on the new page.

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

This project was my first time working with partials. It allows you to create a form that you can call on in different files within that folder. I even made a partial for error messages which was super helpful because I am using that in almost every file.

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

Last thing I needed to accomplish was setting a scope method that took me to a specific route showing the results. At first I made an alphabeticalize method to put my books in alphabetical order. However it did not take me to a new route and I thought that I should do something more challenging. I chose to display the 4 most recently made books. So I made a button on the books index page that takes you to a "most_recent" route and displays the books.

I hope you have enjoyed reading about my project. I know its not perfect and if I tired I could probably break more components. I could figure out ways to fix them but I will save that for another day and celebrate what I have made for now!