Sinatra Project #test_project

Posted by Rene Cocom on October 28, 2019

Video Games have been fun for everyone, and no matter how old the game people still enjoy them and love to share their thoughts. I can enjoy them as well, and get to join communities for discussion game-related topics. Discussion boards are famous because people can find others with like interest in the games they’ve played. They can also save the arguments and opinions in a secure environment and get back to them as they please. Even finding related topics matter for some who haven’t played a particular game before and would like some insight from someone other than an editor. As for Sinatra, I have the ability to create a database where Ruby shines because I am dealing with a database and Object-Oriented related product. I happen to love the ability to share my insight into how games perform and share tips on how to overcome and set obstacles. The idea of a user and post scenario worked for me because this is something I am familiar with and know how it should run, which is something I am looking forward to learning more of.

Lets Get Started!!

My first approach was looking at how I should set up MVC. Within my models are two folders that use has_many and belongs_to. So, I started within my model folders one is gamer, the other is called forum. The forum folder has a class that is inheriting from ActiveRecord::Base inside of my environment folder. The ActiveRecord::Base establishes a connection to my database, this is where the belongs_to comes in, because :gamer relates also to my databases and they work together to adding to a forums table and a gamer’s table. The games have many forums, but the forums belong to one gamer each. My table called PasswordGamer < Active::Migration adds a column of passwords to the table gamer as a string.

Next, is the controller folder called application_controller.rb the class is connected to Sinatra Base from my environment file under ActiveRecord::Base which establishes a connection for my folders, and data behaviors. Next is my configure block that contains a group of sets and an enable. The set variable creates an connection between any given key and value, for instance, my :public_folder if setting the string “public to itself”. As for enable :sessions it works with my active record and connects to the other areas with this scope of folders and files using sessions, especially the session_controller. Next, the session is set to secret with is helpful for encrypting cookies.

My welcome page is used for when I need to redirect is conditions are not met, and my controller implements this here with an get “/”. The get is a receiver that will be the home screen of my web application. Next are my helpers, this helps me with not using user, multiple lines for a login, but instead can set helpers for all of my login and current user. In some cases, helpers are given a separate folder, but I have learned either way works fine unless helpers would be needed in more areas. My next controller if my forums controller is it receives from the application controller including the helpers. This folder is responsible for my index which controls the logged_in gamers and number of forums created by gamer by id. The forums will nest the gamer and forum ids and either shows them one at a time, show all and edit them. The forum controller will either create, read one, read all, update and destroy.

The params are used to connect the view folders which parse through nested hash coming from a form seen in my views folder. The params are important because they assist with date management where they contain data inside of forms. Using params[:id] will, of course, retrieve al the data that is contained which then be used. As for instance, I have Forum.destroy(params[:id]) with will delete all contained by :id.

The folder gamer_controller.rb consists of a get ‘/signup’ and a post ‘/gamers’. The post ‘/signup’ is a more secure way to transfer data between client and server, and get ‘/signup’ can be received as a sign up without post although it can be used, the post method to save the gamer_id is more secure. Then the session[:gamer_id] hash is saved in an instance variable @gamer.id to be used throughout the controllers. The last of the controllers is the session controller, seen often throughout the last few controllers and is used to monitor the and match to the forms the user’s login, and log out. The login as an erb :‘sessions/login’ for data relations from the session.erb. Next there is the post ‘/sessions’ that search the params for the user, and uses an important method called has_secure_password in my gamer model. This authenticates the user’s password identity via bcrypt. Users have the habit of using the same password on multiple platforms, which can lead to security risk. Last the get ‘/logout’ session.destroy as before in Forum.destory(params[:id]) but instead, it only destroy the session, not the stored has data.

The view consists of forms that are useful for params in the was the organize information. For objects such as gamer, there is a name, email, and password because they are the users. The form is seen by its form method and action. The path of its action is “/gamers” , and the method is “POST” again a more secure way to transfer data. The erb can have conditions within them to assist with the behavior of the gamer in this instance. Data is stored labels and inputs, the label needs a for=”title” or any other data needed to have a label. Then the input requires a name, id, and type unless its a button like submit will require a type and value. The index.erb has some erb tag <%= forums.id %> which is helpful for importing outside date form other erb. Also, conditions can be implemented and @gamer instance variable used within the erb. In my index.erb the condition checking the gamer in currently logged in then a message with the user’s name will appear otherwise, another message will prompt the user about what is available to them. Links are used in this erb because with the help of the controller. It can be used the a <a href=””>TEXT</a>, an example is <a href=”/forums/new”></a> and will make a text for the user to click on the navigate through tat links path. There is three folders that exist inside of view with similar functions and these are forums, gamers, sessions.

And Lastly

The erb files after are the layout of the HTML with title, meta, link to css, head, body, footer. Each element shows where the information will appear like body with my nav stack of All Gamer Talk!, My Forums, Join, and Logout on every page. Then there is my welcome.erb that contains a <h1>Welcome to Gamer Forums!</h1> inside of a div class= ”container” for CSS styling purposes. All the data used by the MVC is in three databases with there own unique number format to insure security and id of the databases. The databases are gamer, forum, and password_gamer they are a part of ActiveRecord::Migration that migrates the updates information into tables of columns and rows. There are two tables forums and gamers while password_gamer is there to update the current table of gamers when needed. The method def change will add or remove data, and the create_table is metadata that will create a table and add the information upon migration. The t.sting is short for type.string where the type of data being entered is important so string, text, the integer will translate to the create_table what is being entered. The timestamps will be treated by Ruby and updated manually. This web application includes unique user account attributes, ensures that other users can not edit another forum that isn’t theirs and includes validations. The purpose of creating a gamer was successful, and I would learn how to implement the ability to comment on others’ posts but still not edit, and upload documents securely for a complete forum experience.