The software engineering journey has ramped up quickly. FoodSwiper now has a structure that can be hosted on the web. Building an MVC (Models, Views, Controllers). Sinatra Application was an enlightening process for me. The internet is less of a mysterious toy and more of a tool.
The process of building a Models is streamlined so well that building models with complex relationships and standard accompanying methods now can be done with 5 lines of code where before it had 50+lines. Sinatra can build Models with belongs_to, has_many, has_many_through, and many other associations. Just declare the associations in the appropriate Model’s class and the association is complete along with methods to Create, Find, Read, Update, and Delete objects of the that class.
class Restaurant < ActiveRecord::Basehas_many :photoshas_many :likes, dependent: :destroyhas_many :liking_users, through: :likes, source: :userextend Slugifiable::ClassMethodsinclude Slugifiable::InstanceMethodsend
There is a sample Model that makes a Restaurant class and associates it with 3 other classes.
Creating views using ERB tags and html opened a new world for me. I’ve never built a website before and have quickly fell into the rabbit hole of styling. ERB tags make it possible to perform Ruby logic in HTML code. In many ways it turns a static piece of code, HTML, and turns it into a dynamic website that can display a different site depending on user inputs and interaction.
<% if Helpers.current_user(session).id == @user.id %><form action="/users/<%=@user.slug%>/edit" method="get"><input type="submit" name="Edit" value= "Edit Profile"></form><br><h3>Here are all the restaurants you want to eat at:</h3><ol><% @user.liked_restaurants.each do |restaurant| %><li><a href="/restaurants/<%=restaurant.slug%>"><%=restaurant.name%></a> - <%=restaurant.liking_users.count%> foodies want to eat here</li><% end %></ol><% else %><h3>Here are all the restaurants <%=@user.name%> wants to eat at:</h3><ol><% @user.liked_restaurants.each do |restaurant| %><li><a href="/restaurants/<%=restaurant.slug%>"><%=restaurant.name%></a> - <%=restaurant.liking_users.count%> foodies want to eat here</li><% end %></ol><% end %>
Logic in HTML! Didn’t know that was possible.
The last part of the MVC application is the controller. In my opinion the controller is the workhorse of the application. It is deciding what information is needed from the models and which views to send it to. This is where most of the logic and work is done in the application. Coding out good controllers is the key to the functionality of the application. If something isn’t working the way you intended, chances are the logic in your controllers aren’t quite right.
I’m excited how quickly my web application has grown from an idea to a prototype that I can play with. FoodSwiper is now functional in a browser and has the base functionality I am looking for. My end goal is to build this into a phone app so people can swipe and eat all over the world.