Hello dear coders, friends, and enthusiasts!
Today, we are going to go on with our movie catalog project that we developed in ruby using 'sinatra' web application framework.
You can find the first part of the article here: Part-1
Last time, in the first part of our blog, we developed part of the project. Let's remember what we already have and what is yet left to accomplish.
- movie.rb - Movie class to manipulate the movie object.
- movie_store.rb - A class where we will manipulate storage and retrieval of movies from the storage file.
- app.rb - the main file where we accept requests and redirect HTML content to users
- index.html - Main view file where we will list all movies
- show.html - A file where we will show info about a single movie
- new.html - A file that has a form to submit new movies
This is the main file where we accept requests from users and send back response or redirect an HTML view file back to the user.
For this to happen, we need to have certain endpoints. The endpoints are just URLs that need to be entered in the browser to get a certain response. Endpoints also should be sent using some HTTP method that depends on whether you are trying to get information or trying to submit one. To get the information we use the 'get' method and to send information we use the 'post' method.
First of all, we need to include the required files and initiate a storage file where we will store movies.
Here we include the 'sinatra' web app framework gem, a 'movie' class file, and 'movie_store' file that manipulates the storage and retrieval of movies from the 'movies.yml' storage file.
Now we have to show what endpoints we will have. Here they are in short.
- get: '/movies' - get all movies from the storage.
- get: '/movies/new' - get form to submit a new movie.
- post: '/movies/create' - get parameters for new movie.
- get: '/movies/:id' - get info about a certain movie.
This is where we get a request to list all the movies. We use 'all' method of the 'store' object and serve the 'index.html' file with 'erb' function.
NEW MOVIE FORM
This method just serves the 'new.html' file which we will show a bit later and that just includes a form to submit a new movie.
Here comes the one with the 'post' HTTP method. It means that we accept parameters from the user or form.
By creating a new 'movie' object with the given parameters we give it to the 'store' objects 'save' method. All the parameters are sent in the 'params' hash. And at last, we redirect the user to the '/movies/new' endpoint that serves the 'new.html' file again.
SHOW MOVIE INFO
This is the final one and the one that gets the required movie info from the storage file and it is shown in the 'show.html' file.
Let's take a look at the view files that we need.
I have to tell you that I just included the body part of the HTML documents. They all also include bootstrap to make the output a bit prettier. You can take a look at the full version on the Github repository.
It just includes corresponding fields for 'title', 'director', and 'year' fields.
Here we list the movies by passing through the '@movies' list.
This file just shows the movie info that was passed to this file within the '@movie' object.
I have to thank you all, dear friends, for your patience. I tried to keep it short but to make it clearer I needed to divide it into two parts. I hope so much that you fully understood what is going in the project.
You can find the full code on the Github repository here: Sinatra-movies-demo
Please send me your feedback. Let me know what you liked and what you did not. I would be very glad to hear them and goodbye till we meet in the next article.