(https://github.com/RoUchiha/sinatra-content-management-system-app)
This project was my first experience building something that felt like a real website. Compared to the final project for Phase 1, I found this assignment much easier, most likely because I am much more comfortable with coding now than I was back then.
My web application is a Pokemon Team Creator. A user is given the option to either log in to an existing account, or create a new one. The User Controller validates the presence of all fields in the sign up form and adds the new user to the database, then redirects them to their Trainer homepage. There, they are greeted and shown all Pokemon that they have created and added to their team, and they are also given the option to create a new Pokemon for their team. If a user already has an account, they can login and their credentials are validated before being redirected to the Trainer homepage. If there are incorrect or missing credentials, the user is redirected back to the login page with an error message.
post '/signup' do
if params[:trainer_name] == "" || params[:gender] == "" || params[:email] == "" || params[:password] == ""
erb :'trainers/new', locals: {message: "Please fill out all fields!"}
else
@trainer = Trainer.new(:trainer_name => params[:trainer_name], :gender => params[:gender], :email => params[:email], :password => params[:password])
@trainer.save
session[:user_id] = @trainer.id
redirect "/trainers/#{@trainer.slug}"
end
end
post '/login' do
@trainer = Trainer.find_by(:email => params[:email])
if @trainer && @trainer.authenticate(params[:password])
session[:user_id] = @trainer.id
redirect "/trainers/#{@trainer.slug}"
else
erb :'trainers/login', locals: {message: "Email and/or password is incorrect! Please check credentials and try again."}
end
end
From the Trainer homepage, the user can logout, view their Trainer info, view all Pokemon in the database, or create a new Pokemon. When viewing Trainer info, the user is taken to a page that loads the 'trainer/index' view, displaying the user's Trainer Name and email. When a user views all Pokemon in the database, they are shown every Pokemon that has created by every user, with the name of Trainer that owns that Pokemon next to the Pokemon's name. Creating a new Pokemon loads the 'pokemons/new' view which, upon submitting, creates a new Pokemon and links that Pokemon to the user, and then redirects to the 'show' view for that newly created Pokemon.
Editing or deleting a Pokemon first validates if there is a user logged in, and then checks if the current user is actually the owner of the Pokemon that the user is attempting to edit/delete.
patch '/pokemons/:slug' do
@pokemon = Pokemon.find_by_slug(params[:slug])
@trainer = Trainer.find(session[:user_id])
if params[:name] == "" || params[:type1] == ""
erb :'pokemons/edit', locals: {message: "Please fill out at least the Pokemon's name and primary type!"}
redirect "/pokemons/#{@pokemon.slug}/edit"
else
@pokemon.name = params[:name]
@pokemon.nickname = params[:nickname]
@pokemon.type1 = params[:type1]
@pokemon.type2 = params[:type2]
@pokemon.save
redirect "/pokemons/#{@pokemon.slug}"
end
end
get '/pokemons/:slug/delete' do
@pokemon = Pokemon.find_by_slug(params[:slug])
@trainer = Trainer.find(session[:user_id])
if logged_in?
if @pokemon.trainer_id == @trainer.id
@pokemon.delete
redirect "/pokemons"
else
redirect "/pokemons"
end
else
redirect "/"
end
end
Overall, I enjoyed the process of creating this web application much more than the Phase 1 Ruby CLI project. Everything that was required of me for this project made sense and I did not feel the need to plan anything ahead since it was all so straight-forward. I did however get stuck on a few areas where I tried to add additional functionality beyond the scope of what was required for this project, such as allowing the user to edit their credentials/info, and implementing a "best buddy" feature for when the user creates a Pokemon and wants to make it their "best buddy", which would cause that Pokemon to be displayed on the user's Trainer Info page. I could not figure out how to properly implement a true/false checkbox and then update the new Pokemon's "best buddy" property, so I decided to scrap the whole idea.
I think this project gave me a lot of ideas on how I can turn such a simple web application into something much more complex and robust, and I look forward on learning techniques to do just that.
Top comments (0)