A Organized Resource Sinatra App: My Steps From Stuck-land to Success
Sinatra Content Management System App is quite a mouthful and a bit confusing if you are not in tech, so let’s just say… “hey, I made a website!” With a great deal of preparation (labs, reading, study groups, etc.), plenty of time planning, and a ton of troubleshooting, I was able to successfully complete my second project for my software engineering course at Flatiron. There are many pieces of the project or parts of the project I could focus on, but what I really want to share today are the steps to success for one key aspect of my app.
I say key because it was part of my original plan and in my opinion, an important feature of what the app offered. My intention was to create a website where a user could save a collection of their notes AND have their notes organized by topic. I planned everything out to make sure I was meeting the requirement and creating:
- a Sinatra application
- using Active Record
- making multiple models
- coding at least one relationship (has_msny and belongs_to)
- allowing a user to login, signup, and log out
- giving the user the ability to create, read, update and destroy their resource (this is known as CRUD folks!)
- AND a bunch of validations/authorization I won’t get into in this post
At any rate, a major piece of the picture I had in mind when I imagined my app was an organization aspect of notes by topic. I love information, learning, and yes, even note-taking, but my notes are often unorganized and kept in various places. A few in notepad, some in Google Docs, some on scraps of paper. So when I ran into issues with getting my app to actually organize my notes by topic, I could have given up that part and quickly finished my project, but sticking to my original plan proved to be an incredible learning experience and very rewarding. I know at times it is important to be flexible and change course, but determination, as I am witnessing again and again, is integral in coding life.
I planned and wrote out the steps I would take that made sense to me as a way to approach the project:
- Use the Corneal gem to create my files and folder and Connect to GitHub.
- Create my database and migrations. Make sure to have a foreign_key on the notes table that connects back to the users table.
- Code the models and establish the relationships.
- Code out the Application Controller and set up the homepage.
- Code out the signup, logout, and login. Enable sessions and authentication.
- As I go, create forms and use pry to make sure routes work.
- Create a controller for the resource that belongs to a user and code the CRUD!
- Make sure authorizations are in place.
So I happily coded along and debated whether to have two topics or three. I went with two, which I learned would be my first major issue in getting my app to organize like I wanted it to. I originally made “topic” an attribute of my Note class. So my first step to success was to make new migrations to change the topic attribute into topic_id (on the notes table since a note would belong to a topic and a topic would have many notes) and change the type from string to integer. I had to also create a topics table, which just had a name attribute.
Once I completed my new migrations I was able to create my Topic class, set up the relationships, and set up a topic model, which really did not need to do much except inherit from Active Record so it could use the methods I would need to make my code work. For example, using the has_many method which then allowed for calling .notes on topic would be integral.
A good deal of my troubleshooting came into play in my notes controller and my .erb files. I figured out I would need to “group” my topics, but was still getting stuck. Thankfully I received some pointers for my cohort lead (don’t be afraid to ask for help!), which showed me I was on the right track and I just had to figure out the right syntax for the controller. Knowing how to navigate and use pry was one of the most useful tools when working through these stuck places, and really for this whole project. Then for the view/.erb file I had to do a lot of troubleshooting and trial and error to make the notes organize correctly. First the topics repeated with each note, which I only wanted the topic to list once and each corresponding note to list after it. Then I got the topics to list (just the current user, which was a necessary part of authorization for my particular app), but then all the notes for all users under that topic were listed.
With some more troubleshooting using shotgun and pry, I was able to get what I wanted. I had to iterate on @topic only and then add a second iteration with the addition of .notes and an if statement. One final piece of the puzzle was that since my index.erb view was where I routed a user after logging in, I had to code out a message to appear for a user that hadn’t created any notes yet. I did not want to take a new user or a user who had not saved any notes to a page that said “Notes by #{current_user.name}” and have the page just be empty. This is what I came up with that worked for me.
I cheered very loudly and may have done a happy dance. One last additional accomplishment was seeding my database with topic objects and using them to create a beautiful selection/drop-down menu of topics for my user to choose from. Without seeding, I could not have persisted data to my database to use for my project or test aspects of my project.
Coding has shown me that there will be many stumbling blocks and stuck places along the way, but perseverance and fighting for what you want your app/project/fill-in-the-blank to do, will certainly pay off. It may be frustrating at times, but figuring it out and making what you had in mind work is one of the most rewarding experiences. You will be taken to a place in your mind that brings you joy. And I guarantee you will learn a great deal along the way.
Takeaways-
- Plan out your flow and your steps
- Use those steps!! Go slowly, step by step when you code this project
- Understand Active Record migrations and use pry to practice using Active Record methods as you code your project.
- Know how to seed your database.
- DO NOT attempt this project without knowing pry and throwing in many-a binding.pry as you walk through this project. Use it in each new route.
- If something is meaningful in how you envision your project, do not give up on it.
- Take breaks, ask for help if you get stuck, and celebrate your victories.