DEV Community

Cover image for My first MVC app..
Rylie Spriggs
Rylie Spriggs

Posted on

My first MVC app..

As I set out on Phase 2 at Flatiron, I knew that my next project would be even more complex than my CLI project.

MVC.. Models, Views, and Controllers, all moving parts of an intricate machine. To put it bluntly, I did struggle with concepts and how each gear moved with one another, but I never gave up. I marched on through lab after lab to get to my Sinatra Project which proved to test me in multiple ways, however I learned much more about programming and also much more about myself.

My MVC was based around the career I love, making coffee. Each day I go to work doing what I love, making morning fuel for every day people, I take pride in my work and even more so when I come up with a drink that everyone loves. This inspired me to create an app that will allow user's to view already created drinks by a pro or the user can create a drink on their own and use it for a future order!

To make this application work, structure is the most important thing:

  • app

    • controllers
      • application_controller.rb
      • users_controller.rb
      • drinks_controller.rb
    • models
      • users.rb
      • drinks.rb
    • views
      • users
        • signup.erb
        • login.erb
        • account.rb
      • drinks
        • create_drink.erb
        • index.erb
        • show.erb
        • edit.erb
      • welcome.erb
      • layout.erb
    • public
      • css
      • images

First created the models: user and drinks
user has_secure_password
user has_many :drinks
drinks belong_to :user

Now for controllers, User can login, or signup, or logout.

get '/signup' do
if logged_in?
redirect to "/users/#{@user.slug}"
else
erb :'users/signup'
end
end

get '/login' do
if logged_in?
user = User.find_by(id: session[:user_id])
redirect to "users/#{user.slug}"
else
erb :'users/login'
end
end

get '/logout' do
if_not_logged_in
session.clear
redirect to '/'
end

Drinks controller can create drink, edit, or delete a users drink.

get '/drink/new' do
if logged_in?
erb :'drinks/create_drink'
else
redirect '/login'
end
end

patch '/drinks/:id' do
@drink = Drink.find_by_id(params[:id])
if params.empty?
redirect "/drinks/#{@drink.id}/edit"
elsif logged_in? && !params.empty? && current_user.drinks.include?(@drink)
@drink.update(name: params[:name], size: params[:size], flavor: params[:flavor], milk: params[:milk], toppings: params[:toppings], details: params[:details], user_id: params[:id])
redirect "/drinks/#{@drink.id}"
else
redirect '/login'
end
end

delete '/drinks/:id' do
@drink = Drink.find_by(id: params[:id])
@drink.destroy
redirect '/drinks'
end

With controllers set in place, our views come along with it.
A user can sign in with a user name and password or login. Then, redirect to their account.

A user's drinks can be created, edited, and deleted.

All files end up compiled together to create a working app,
of course with CSS to make it aesthetically pleasing!

The whole process of project planning, creating, testing, and debugging was a long road, but at the end of the day I'm grateful to have made such a project and be able to call it my own.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay