I just recently created EcookBook! This web application was built by utilizing the Sinatra framework. The purpose of the app is to store recipes and to also find new ones. During my journey there were some interesting discoveries I made that I think others would find useful. Let’s explore those topics.
Hello Forms!
The internet is basically just a big form. For those who aren’t familiar with forms, they are what allows any website to get and use user inputted data. Log in pages, twitter post, search fields on google, and shopping carts/checkout are all examples of forms. I used forms for logging in, signing up, creating recipes, editing recipes, and creating accounts. While this is pretty standard protocol for websites, I had to make one form a little special to work with my application. I had a many-belongs to relationship between Users and Recipes. That means whenever I create a recipe I need to make sure that the user_id gets assigned to that recipe. Generally, forms gets the data the user inputs and stores it in what is called params (basically a hash with user data). I can’t have an input on my form for the user to store this critical information. This would open me up to all kind of potential problems. I wanted to make sure this happened behind the scenes. The way I fixed this was by only letting someone create a new recipe that was logged in. Being logged in gave me access to a session[:user_id]
. I created an input just like I did for everything else in my form except for two things.
- I gave this input a preset value of
session[:user_id]
. -
input type=“hidden”
This did two big things for me.
First, it allows me to know what user created this form and secondly, with the type being hidden, the user never sees this on the form. My params then had all the information I needed to create a new recipe.
Dynamically not Dynamic!
When creating this application. I wanted it to be pretty basic. I did not want to include any javascript and just wanted to use css for my styling. This was a challenge because I wanted to display my recipes in a user friendly way. I wanted the ingredients and steps to be listed out.
The forms I created were static. How can I get a list of ingredients and steps with each recipe having a different number of inputs. It would require me to have tons of of static forms with different number of input fields. This was not practical, so I came up with a little work around using helpers and the power of ruby! Helpers are methods you can create to perform actions that can be used anywhere. To make my static form dynamic, I required the user to input their information very specifically.
I game them a list of examples and also used helper methods to make sure the user entered the information correctly or they get an error saying what they did wrong. I had them enter the information with and “*” between each ingredient or step. This gave me a large string in my params that I could then use ruby to manipulate.
I split the string by the * and created a new array with each element being a step or ingredient. Then on my views file I used embedded ruby to iterate over the array and show each element in a beautiful list. This allows the users to enter as many steps are they want and it dynamically show them a styled recipe.
EcookBook was a very fun application to build. Just like anything else in programing there is a lot of ways to do something. Finding little work arounds and tricks to get your project working is what makes programming fun! I hope these little discoveries I made can help you on your next project!
Top comments (0)