Sinatra

1,973 views

Published on

Introduction to Sinatra

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,973
On SlideShare
0
From Embeds
0
Number of Embeds
1,080
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Rake is a generic ruby library for building web applicationsRail3 has featured support for sinatra web applications
  • Sinatra

    1. 1. SINATRANitesh KumarMember of technical staff11.12.12
    2. 2. ROADMAP What is sinatra? Why sinatra? Flexibility with sinatra Singing with sinatra File hierarchy and naming convention Features Sinatra with ActiveRecord
    3. 3. WHAT IS SINATRA?  A micro-framework for web applications  A ruby domain specific language  A ruby “gem”
    4. 4. WHY SINATRA?  Lightweight  Nicely pluggable within other application  Good tool for creating REST-like interface  Support many different template syntaxes.
    5. 5. SINATRA ON RAILS#in routes.rbmatch “/blog” => MySinatraBlogApp, :anchor => false#Sinatra appClass MySinatraBlogApp < Sinatra::Base #this now will match /blog/archives get “/archives” do “My old posts” endend
    6. 6. FLEXIBILITY WITH SINATRA :  Template engine  ERB, HAML, Builder..  ORM  ActiveRecord, DataMapper, Sequel  JS library  jQuery, Prototype, YUI, Dojo  Testing tool  Test::unit, Rspec, Bacon
    7. 7. LET‟S START THE SHOW  gem install sinatra  ruby hello-world.rb
    8. 8. SINGING WITH “SINATRA”#sample.rbrequire „rubygems‟require „sinatra‟get „ / ‟ do “Why this kolavari kolavari kolavari di..”end
    9. 9. SO Every url you wants to respond to goes in your .rb file Pages themselves goes in /views as .erb files Wrapping layout goes in /views/layout.erb Static files goes in public |-- beta.rb |--public | `--stylesheets | `--shared.css `-- views |--index.erb |--layout.erb
    10. 10. FEATURES: [ ROUTING ] Routing to code blockget „ / „ do “Hello world!” “ block ”end Whatever is returned from the block is sent to the browser
    11. 11. FEATURES: [ VIEWS ]#beta.rbrequire „rubygems‟require „sinatra‟get „ / ‟ do erb: indexend#views/index.erb<h3> Welcome the my website </h3><p>I‟m being developed . Thanks for stopping by.</p>
    12. 12. FEATURES: [ LAYOUTS ]#views/layout.rb<!DOCTYPE html><html> <head> <title>Beta Info</title> </head> <body> <%= yield %> </body></html>#views/index.erb<h3> Welcome the my website </h3><p>I‟m being developed . Thanks for stopping by.</p>
    13. 13. FEATURES: [FILTERS]beforebefore do MyStore.connect unless MyStore.connected?endget / do @list = MyStore.find(:all) erb :indexendafterafter do MyStore.disconnectend
    14. 14. FEATURES: [HELPERS]helpers do def bar(name) "#{name}bar“ endendget /:name do bar(params[:name])end
    15. 15. DYNAMIC URLS?#dynamic.rbrequire „rubygems‟require „sinatra‟get „ / ‟ do “This is homepage.”endget „/:name‟ do „Hello‟ + params[:name] + „!‟end
    16. 16. SINATRA WITH ACTIVERECORDrequire „rubygems‟require „sinatra‟require „active_record‟class Article < ActiveRecord::Baseendget „ / ‟ do Article.establish_connection( :adapter => “sqlite3”, :database => “hw.db” ) Article.first.titleend
    17. 17. REFERENCES http://sinatra-book.gittr.com/ http://www.sinatrarb.com/ http://www.slideshare.net/Cannikin/classy-web- development-with-ruby-sinatra-and-heroku-1
    18. 18. THANKS!

    ×