O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Sinatra

Book Description

Design and deploy your own web application in minutes using Sinatra

About This Book

  • Exploit Sinatra to design a web application
  • Integrate external, internal, and inline CSS and use some popular CSS frameworks
  • Deploy an application onto your own server or on a cloud application platform

Who This Book Is For

Learning Sinatra is a great place to start your first Sinatra application. It doesn't matter whether you have written a web application before or not, though some basic knowledge of programming is essential.

What You Will Learn

  • Install and set up Ruby and Sinatra
  • Work with layouts and template routes
  • Extend the code to include CSS in different ways
  • Create projects and understand MVC
  • Design the front-end with Twitter Bootstrap
  • Use the MySQL/SQLite gem to communicate with databases
  • Implement sessions that can be used across multiple pages
  • Utilize the innovative and user-friendly features of ORMs

In Detail

Sinatra is a Ruby framework that is widely used in the Industry. You can use it to make a single-page web app or a large-scale one. With the increased online footprint, you can create and deploy your own application.

Whether you are brand-new to online learning or a seasoned expert, this book will provide you with the skills you need to successfully create, customize, and deploy a Sinatra application. Starting from the beginning, this book will cover how to install Ruby and Sinatra, construct the back-end, design and customize the front-end layout, and utilize the innovative and user-friendly features of ORMs. By sequentially working through the steps in each chapter, you will quickly master Sinatra's features to create your own application.

With ample screenshots and code that offers a play-by-play account of how to build an application, Learning Sinatra will ensure your success with this cutting-edge framework.

Style and approach

This book takes a step-by-step approach to Sinatra and explains it in a conversational and easy-to-follow style. A simple project is used as a base to explain each topic. The code and screenshots included add to the ease of learning.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Learning Sinatra
    1. Table of Contents
    2. Learning Sinatra
    3. Credits
    4. About the Authors
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Introduction to Sinatra
      1. What is MVC?
      2. Installing Sinatra
      3. Writing your first Sinatra application
      4. Where is the MVC in this code?
      5. How to achieve MVC using Sinatra
      6. Sinatra application layout
      7. HAML
      8. Installing HAML
      9. Using HAML in a Sinatra application
      10. What did the code do?
      11. To Sinatra or not to Sinatra?
      12. Other frameworks
      13. Summary
    9. 2. Introduction to Ruby
      1. Installation
        1. For Linux users
        2. For Windows users
      2. IRB
        1. Installation
        2. Using IRB
      3. Ruby data types
        1. Strings
        2. Numbers
        3. Symbols
        4. Arrays
          1. Array.each
          2. Array.include
          3. Array.map
          4. Array.select
        5. Hashes
          1. Hash.each
          2. Hash.include?
          3. Hash.keys
          4. Hash.select
      4. Code blocks
      5. Classes and objects
        1. Defining modules and classes
        2. Constructors
        3. Class methods and instance methods
          1. Defining methods
            1. Methods with no arguments
            2. Methods with a fixed number of arguments
            3. Arguments with default values
            4. Methods with variable-length argument lists
        4. Private, protected, and public methods
      6. Control structures in Ruby
        1. if-elsif-else-end
        2. unless..end
        3. Ternary operator
        4. Inline conditions
        5. Case
        6. Operators
          1. eql?
          2. equal?
      7. Loops
        1. while-end
        2. until-end
        3. Fixnum.times
        4. for..in
      8. Controlling the flow of iterations
      9. Exception handling
      10. Summary
    10. 3. Hello World
      1. Getting started
      2. The application
        1. The idea
        2. The file structure
          1. app.rb
          2. config.ru
          3. Gemfile
          4. Bundler
          5. Gem install bundler
          6. Bundle install
          7. lib/routes.rb
          8. What is a route?
      3. The code
        1. app.rb
          1. Breaking down the code
        2. config.ru
          1. How the code works
        3. Gemfile
      4. Starting the application
      5. Features
        1. Users
        2. Lists
      6. Object Relationship Mapper
      7. Designing views
      8. Summary
    11. 4. Modeling the Backend
      1. What is Object-relational mapping
      2. When to use an ORM
      3. How to define migrations
        1. The users table
        2. The lists table
        3. The items table
        4. The permissions table
        5. The logs table
      4. How to define models
        1. The User model
        2. The List model
        3. Permission Model
        4. The Log model
      5. How to run migrations
      6. What are the responsibilites of the models?
        1. The User model
        2. The List model
        3. The Item model
        4. The Permission model
        5. The Log model
      7. Summary
    12. 5. Handling HTTP Requests
      1. Understanding the application file – app.rb
        1. Writing app.rb
      2. Writing routes for the responsibilities of the application
        1. Showing a page for all the existing lists
        2. Showing a page to create a new list
        3. Handling the saving of a new list
        4. Showing a page to edit a list
        5. Handling the updating of a list
        6. Handling the changing of permissions on a list
        7. Showing a page for signup
        8. Saving the new user data
        9. Showing a page for login
        10. Handling user login
      3. Sending the data and designing the views
        1. Showing a page for all the existing lists
        2. Showing a page to create a new list
        3. Handling the saving of a new list
        4. Showing a page to edit a list
        5. Handling the updating of a list
        6. Changing permissions on a list
        7. Showing a page for signup
        8. Saving the new user data
        9. Showing a page for login
        10. Handling user login
        11. Handling user logout
      4. Understanding before and after blocks
      5. Summary
    13. 6. Designing the Frontend Layout
      1. Understanding Twitter Bootstrap and jQuery
      2. Writing haml templates
      3. Writing layout.haml
      4. Writing signup.haml
      5. Summary
    14. 7. Handling User Data
      1. Creating forms
        1. A quick overview of the HTML form tag
        2. Writing new_list.haml
        3. Handling data in the backend
        4. Validations
        5. Frontend Validations
        6. Backend Validations
      2. Summary
    15. 8. Connecting to a Database
      1. Connecting to a database
        1. An interactive console
        2. How to use an ORM
        3. The Sequel model
        4. Some useful model class methods
          1. db_schema
          2. columns
          3. create
          4. find_or_create
          5. def_column_alias
          6. first, first!, and last
          7. with_pk and with_pk!
          8. finder
          9. no_primary_key
          10. set_primary_key
          11. restrict_primary_key and unrestrict_primary_key
          12. set_allowed_columns
          13. subset
        5. Useful model instance methods
          1. values or to_hash
          2. errors and valid?
          3. exists?
          4. changed_columns
          5. refresh or reload
          6. modified?
          7. new?
          8. modified!
          9. save
          10. delete
          11. destroy
          12. set
          13. set_all
          14. update
          15. update_all
        6. Hooks or callbacks
        7. Associations
        8. Eager loading
        9. Joins with associations
      2. Summary
    16. 9. Authentication and Authorization
      1. Using sessions in Sinatra
        1. What are sessions?
          1. How does it work?
          2. Using the session variable
        2. Enabling a session
          1. Advanced options
        3. Methods of the session object
      2. Handling authentication using session
        1. What is authentication and authorization?
          1. Building authentication
          2. Building authorization
      3. Summary
    17. 10. Deploying the App
      1. Deployment
      2. What is Heroku?
        1. Getting started with Heroku
        2. Creating an account
        3. Setting up the command-line interface
        4. Creating a Heroku app
        5. Setting up an app for Heroku
          1. Adding Ruby to Gemfile
          2. Adding Procfile
      3. Using SQL on Heroku
      4. Summary
    18. Index