GithubHelp home page GithubHelp logo

sinatra-hacker-news-clone's Introduction

Background & Objectives

Congrats, you are now an expert in ActiveRecord :) Let's take a step back from what we've learnt so far since the very first day and the setup:

  • Store information in variables
  • Define methods to implement generic behavior on arguments and reuse code
  • Use several types, simple (Fixnum, String) or complex (Hash, Array)
  • Use conditional branching with if
  • Loop over collections with for, while or Enumerable#each

With that, we have the basics of any programming language. If you understand those concepts, then you now are a programmer. And you can pick up really quickly any new object oriented language, just by understanding how the stuff above works in the new one. You'd have to learn a new syntax, not new concepts.

You also learnt more complex stuff, to help us build big software.

  • Class, to encapsulate data and behavior in an object
  • MVC, to architect a software where classes have a single responsibility
  • ActiveRecord, a layer on top of the database to abstract SQL queries (write Ruby code instead of SQL)

We are getting really close to Rails. What's missing, and you know it, is the View level. We're supposed to build websites, not command line tools! Where's the HTML?! CSS?!

Let's play with the Sinatra gem for a preview of how awesome it will be!

Setup

Install the gems specified in your Gemfile with the following command:

$ bundle install

We already give you the migration and the seed. Run them with:

$ rake db:drop db:create db:migrate db:seed

Launch the sinatra app.

$ ruby app.rb

Look! You can go to http://localhost:4567. You are running a small webserver and query it with your browser. No more command line!

Some words about Sinatra

The app.rb file acts as the controller. The router layer is handled by Sinatra. We already created a controller method to handle the root of the web app. Sinatra maps the URL in the browser to the right method in app.rb, look at the routing doc.

# app.rb
# [...]

get '/' do  # <- Router part

  # [...]   #
  # [...]   # <- Controller part
  # [...]   #

end

Read about Views, Routing, params here before starting coding.

Specs

This exercise is quite open, here are a few things you can start with:

  • Display all posts on the homepage of the site
  • Posts should be clickable, open a new tab and go to the website
  • Display posts in descending vote order (see scopes)
  • [Hard] Add a form at the top to submit a new post (hint: use a post route in app.rb)
  • [Very Hard] Add a way to vote on a post.

Have fun!

There's no tests for this exercice, so rake will just run Rubocop do make sure you have a good style.

Sharing

Don't hesitate to share you work on Slack with ngrok. Install ngrok (with brew cask install ngrok or manually for Ubuntu), and run it in another window.

$ ngrok http 4567

You should get a publicly browsable URL (*.ngrok.com) to share with everybody!

sinatra-hacker-news-clone's People

Watchers

Sylvain Peigney avatar James Cloos avatar

Forkers

saravanan12153

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.