Giter Site home page Giter Site logo

sinatra-multiple-controllers-london-web-051319's Introduction

Sinatra Multiple Controllers

Overview

In this lesson we'll cover separating domain concepts into separate controllers.

Objectives

  1. Describe the structure of a multi-controller application
  2. Separate domain concepts in our code into separate controllers
  3. Mount multiple controllers in config.ru file using run and use commands

Separating Out Our Controllers

If you think about a typical e-commerce application, you would need at least two models: one for orders and one for products. The products model would require a series of routes all starting with /products, and the orders model routes would start with /orders. /products/1 would represent requesting information about the very first product, and /orders/1 would represent requesting information about the first order.

When you think about all the CRUD actions, you would need a lot of controller actions for both of these models.

Products:

Request Route CRUD Action
GET '/products/:id' Read
GET '/products/new' Create
POST '/products' Create
GET '/products/:id/edit' Update
POST '/products/:id' Update
GET '/products' Read
POST '/products/:id' Delete

Orders:

Request Route CRUD Action
GET '/orders/:id' Read
GET '/orders/new' Create
POST '/orders' Create
GET '/orders/:id/edit' Update
POST '/orders/:id' Update
GET '/orders' Read
POST '/orders/:id' Delete

With GET and POST requests, you're looking at a really full and complex controller, with 7 controller actions for each model. That's 14 controller actions before you even add in users or shopping carts. With that much code in one file, it can get hard to maneuver and find different pieces of the code you might need to edit or update.

Just like we separate out our different models into different files, we need to separate these domain concepts in our code into separate controllers. Every controller in our application should follow the Single Responsibility Principle, only encapsulating logic relating to a singular entity in our application domain. We need to separate out a Products Controller and an Orders Controller.

Products Controller

You can imagine a controller ProductsController, defined in app/controllers/products_controller.rb.

class ProductsController < Sinatra::Base

  get '/products' do
    "Product Index"
  end

  get '/products/:id' do
    "Product #{params[:id]} Show"
  end

end

Orders Controller

Similarly, we'd have OrdersController defined in app/controllers/orders_controller.rb.

class OrdersController < Sinatra::Base

  get '/orders' do
    "Order Index"
  end

  get '/orders/:id' do
    "Order #{params[:id]} Show"
  end

end

Mounting Multiple Controllers in config.ru.

Now that our application logic spans more than one controller class, our config.ru that starts our application becomes a bit more complicated.

First, our environment must load both controller files.

config.ru:

require 'sinatra'

require_relative 'app/controllers/products_controller'
require_relative 'app/controllers/orders_controller'

Then, we must mount both classes. Only one class can be specified to be run. The other class must be loaded as Middleware. We won't get into MiddleWare now, suffice to say, you simply use it instead of run.

config.ru:

require 'sinatra'

require_relative 'app/controllers/products_controller'
require_relative 'app/controllers/orders_controller'

use ProductsController
run OrdersController

Which classes you use or run matter, but we won't worry about that now, just make sure you only ever run one class and the rest are loaded via use.

You would start your server in the same way you would any Sinatra application. Both shotgun and rackup work just fine!

Video Review

NOTE these videos appear later in the Sinatra curriculum.

Resources

sinatra-multiple-controllers-london-web-051319's People

Contributors

annjohn avatar ipc103 avatar drakeltheryuujin avatar victhevenot avatar maxwellbenton avatar franknowinski avatar jmburges avatar imkaruna avatar walwoodr avatar rrcobb avatar sdcrouse avatar itsjustarepo avatar

Watchers

Kevin McAlear avatar  avatar  avatar  avatar Belinda Black avatar Bernard Mordan avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.