Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

GDI Ruby Class 3: Sinatra

on

  • 941 views

 

Statistics

Views

Total Views
941
Views on SlideShare
941
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

GDI Ruby Class 3: Sinatra GDI Ruby Class 3: Sinatra Presentation Transcript

  • Ruby and Sinatra Emma Persky
  • Sorry for Weird Slides• I bought the wrong laptop on holiday with me, so i tried to remake them today... better slides will appear when I return on Thursday
  • Housekeeing : Using an Editor• At some point it becomes important to edit files with a decent editor, as extensions and filetypes become important• TextMate for OS X• Notepad++ windows• Ask TAs about others
  • RubyGems• A way of distributing reusable code• Can be as simple as a few lines• Or as complex as Ruby on Rails
  • Install some gems• gem install sinatra• gem install sqlite3-ruby• gem install dm-core• gem install dm-sqlite-adapter• gem install dm-migrations• windows users will need the sqlite dll (see http://tinyurl.com/25koqcq)
  • Why Sinatra• Because we can have a web app in ONE file (yes, one file) - we don’t need to understand controllers, or routing, or models, app structure, anything like that• Really great for simple web applications• Easy to learn• Extends to big applications to (we use it for lots at purpose, and myself personally)
  • Basic Sinatra Apprequire rubygemsrequire sinatraget / do  "Hello World"end save this in a file called gdi1.rb then in a console navigate to the directory you saved it in and run “ruby gdi1.rb” then visit http://localhost:3000https://gist.github.com/709555
  • • When you run a sinatra app it should tell you what port it is running on (normally 3000)• Each time you run a sinatra app, end it with “control-c” before running it again• For the moment, if you make changes, kill the app and start it again
  • How did that happen?• we defined the route “/” also known as the default root and asked it to respond to “get” http verb.• we then told the route to respond with “Hello, World!”• When we run the file with ruby, Sinatra started for us, and win!
  • HTTP VERBS• There are 4 verbs we need to know about• GET, PUT, POST, DELETE• They are way of telling the web server to act in a certain way, and our app can respond to the same url in different ways with different verbs
  • more about verbs• GET is for “getting” a resource from the server (a web page, a file, etc), and used when we don’t change any data• POST is used when want to change something on the server• There are also PUT AND DELETE, but they are not important just yet
  • PARAMETERS• Just like functions took parameters, so can actions in our Sinatra app• They can be in one of three forms• In the URL (/animal/1) (param is 1)• In the query string (/animal?id=1)• In the Post Body (when using forms)
  • in the url • denominated in the url with a colon (:) • then accessed in the method using params[parameter_name_with_colon] eg get ‘/say_hello/:name‘ params[:name]https://gist.github.com/710399
  • what about html • Sinatra gives us access to “templates” • We can put them at the end of the file, each on starts with a “@@ template_name” • We are using “erb” templates, which have #{} style string replacement with this special syntax <%= ... %> • We can pass parameters to templates by setting @ variables in the action • Finally we tell Sinatra to use the template using “erb :template_name”https://gist.github.com/710403
  • form submission • forms are a pretty critical part of most web applications (collecting data from users) • and with sinatra they are easy :) • we probably want to “post” to a form handler, so instead of “get ‘/ some_action’ we will use “post ‘/ some_action’”https://gist.github.com/710415
  • MORE FORMS• form elements become “parameters” and we access them in the same way.• a form element with a name “foo” is access with params[:foo] in your action• remember to set your action up with “post”
  • Layouts• sometimes we use the same html over and over again in our websites at the top and bottom of every page. we call this the “layout”.• instead of typing it out for each template, sinatra and erb give us a way of typing it only once.• we use a special syntax <%= yield %> to tell sinatra and erb to put the real template contents in
  • more layouts • now our main templates are simple, and only have specific html for the, and sinatra and erb put them in the layout where they find <%= yield %>https://gist.github.com/720257
  • classes • we can use classes in sinatra applications as easily as we did before • simple include the class code at the top and then use as before • in our case, when we POST to ‘/animal’ we create a “new” animal (remember, new), we save it into our @animal variable, and then use that in our templatehttps://gist.github.com/710431
  • YOUR TURN!• Edit the last file to do the following:• Add new properties to our animal (hint attr_accessor)• Set those properties when we create a new animal (hint def initialize)• Collect that information on the form• Display that information after we click submit (instead of urrr?)
  • Next week• class is on monday as usual• we are going to go crazy and cover two big topics: databases and deployment• yes, your app will be live on the interwebs!• please be ontime as there is lots to cover• as usual any questions email emma.persky@gmail.com