Git Product home page Git Product logo

messages-lab's Introduction

#iChat using Sinatra and Active Record

Objectives

  • integrate Active Record with Sinatra
  • make an http request from a script (rather than a web browser)
  • use a model to access data from a database
  • display data from a database in a view

Resources

Instructions

Setup

--note: '$' means command should be run on command line --

  • fork this repo git clone
  • $cd repo_name
  • $bundle install
  • $rake db:migrate
  • $shotgun
  • Open up a second terminal tab and run $tux
  • In the post_message.rb script modify the uri variable to match your the port your server is running on. For Example:
  == Shotgun/Thin on http://127.0.0.1:9393/
  >> Thin web server (v1.5.0 codename Knife)
  >> Maximum connections set to 1024
  >> Listening on 127.0.0.1:9393, CTRL+C to stop
  • In the above example '127.0.0.1:9393' is your uri

Posting a Params hash from a ruby script

  • first try posting a hash to a url Typhoeus.post(uri, :body => {:key => 'value'}) and put the debugger into your post route. Then, in the console print out "params." You can do this in your post_message.rb script like this:
  uri = "127.0.0.1:9393/"
  Typhoeus.post(uri, {key: 'value'})
  • When you do, you should {key => 'value'} printed in the debugger.
  • You access this is the same way that you do when using a form params[:key] #=> 'value'
  • Now, using Typhoeus gem, post the sender, receiver and message variables to the post '/'

NOTE: When doing this, keep in mind what the params hash looked like when your input came from a form, this is no different except you are directly passing the hash to the url from the ruby script.


  • Modify the post '/' route to create a message based on user input from the post_message.rb script

HINT: Thanks to active record magic, you don't have to assign them all individually. Take a look at the create method


  • Modify the get '/reset' route to delete all messages in the database.
  • At this point, in another terminal tab you should be able to run the post_message.rb script and it should successfully post to the server.
  • You will know it works by going into the 'tux' terminal and typing Message.all. You should see the message that you sent.
  • Now, modify the messages view to display all of the messages in the database.
  • Your resulting view should look something like this:

Imgur

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.