Giter Site home page Giter Site logo

rack-dynamic-paths-readme-online-web-ft-031119's Introduction

Dynamic URL Routes

Objectives

  1. Explain how dynamic routes prevent web applications from having to be rewritten as new information is added
  2. Create dynamic routes

Why Dynamic Routes?

When you create a new repository on GitHub, how do URLs like github.com/jmburges/my-repo get generated? In our current examples, we would have to create a new if statement for each possible URL path. Since this is a dynamic application, our application can't be rewritten every time a new user signs up. So the concept of "dynamic routes" was created.

Setting Up Dynamic Routes

Let's assume we have a playlister app which has an array of Songs. First let's look at our Song object

#song.rb

class Song

  attr_accessor :title, :artist

  def initialize(title, artist)
    @title = title
    @artist = artist
  end

end

Pretty simple class. Now we have our web app.

class Application

  @@songs = [Song.new("Sorry", "Justin Bieber"),
            Song.new("Hello","Adele")]

  def call(env)
    resp = Rack::Response.new
    req = Rack::Request.new(env)
    
    @@songs.each do |song|
      resp.write "#{song.title}\n"
    end

    resp.finish
  end
end

We want more information about each song though. Similarily to GitHub, we want to be able to go to a URL like localhost:9292/songs/Sorry and get all the information on Sorry. We are doing routes like this instead of just plain GET params because it's easier to read. Remember the path is given to us as a string. We could therefore write something like this:

class Application

  @@songs = [Song.new("Sorry", "Justin Bieber"),
            Song.new("Hello","Adele")]

  def call(env)
    resp = Rack::Response.new
    req = Rack::Request.new(env)

    if req.path=="/songs/Sorry"
      resp.write @@songs[0].artist
    elsif req.path == "/songs/Hello"
      resp.write @@songs[1].artist
    end

    resp.finish
  end
end

This is silly though, because every time we create a new Song we would have to create a new if statement. Thankfully, because paths are strings, we can do a regex match against the path. Then we just grab the content after the /song/ to figure out which Song our user would like.

class Application

  @@songs = [Song.new("Sorry", "Justin Bieber"),
            Song.new("Hello","Adele")]

  def call(env)
    resp = Rack::Response.new
    req = Rack::Request.new(env)

    if req.path.match(/songs/)

      song_title = req.path.split("/songs/").last #turn /songs/Sorry into Sorry
      song = @@songs.find{|s| s.title == song_title}

      resp.write song.artist
    end

    resp.finish
  end
end

Now our routes are dynamic! We can just add songs, and everything else is taken care of and works for us. You have written a lot of Ruby; take comfort in your skills.

View Dynamic URL Routes on Learn.co and start learning to code for free.

rack-dynamic-paths-readme-online-web-ft-031119's People

Contributors

annjohn avatar andrewkleinonline avatar brennenawana avatar curiositypaths avatar campbelllsssoup avatar

Watchers

Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar  avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton 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.