1. Install sinatra gem

gem install sinatra --no-ri --no-rdoc

2. Basic App

#!/usr/bin/ruby
require 'sinatra'
get '/' do
"Just Do It"
end

ruby低于1.9,需要在文件开头加require 'rubygems'

ruby basic.rb

Open up your browser and go to http://localhost:4567.

3. Inline Template

Slim is a fantastic template engine that makes this a much easier task.

Install slime: $ gem install slime

#!/usr/bin/ruby
require "sinatra"
require 'slim' get '/' do
slim:index
end #Inline templates always come after the __END__ declaration, and each template begins with @@. __END__ @@layout
doctype html
html
head
meta charset="utf-8"
title Just Do it
link rel="stylesheet" media="screen,projection" href="/style.css"
/[if lt IE 9]
script scr="http://html5shiv.googlecode.com/svn/trunk/html5.js"
body
h1 Just Doi it
== yield @@index
h2 My tasks
ul.tasks
li Get Milk

"@@layout" template: This will automatically be rendered with every view and provides a basic HTML5 scaffolding. The key line in the layout template is right at the end (==yield). The yield statement renders the content from the whichever template was requested by the handler (in this case, ‘index’).

4. Extend Views (把视图分离出来)

5. Dynamic Content

在主文件rb中增加代码

get "/:task" do
@task=params[:task].split('-').join(' ').capitalize
slim :task
end

让‘@task’ equal to the value of params[:task]

对应的视图文件task.slim

h2 My Tasks
= @task

其中@task匹配对应的URL。

6.Forms(窗体处理)

本例把index.slim的内容替换成

form action="/" method="POST"
input type="text" name="task"
input.button type="submit" value="New Task >>"

这样会在“/”页面显示一个窗体(一个文本框、一个提交按钮)

提交的内容需要一个handler来处理,再sinatra文件中用post(对应窗体提交method),代码如下:

post '/' do
@task = params[:task]
slim :task
end

[sinatra] Just Do It: Learn Sinatra, Part One Darren Jones的更多相关文章

  1. 【sinatra】安装测试

    $ gem install sinatra 测试: $ subl app.rb app.rb内容: require 'sinatra' get '/' do "Hello, World!&q ...

  2. docker镜像的操作

    在主机上列出镜像 sudo docker images 每从Docker Hub下载一个镜像就会启动相对的创建一个容器 在镜像列表中看到三个重要的东西: 来自什么镜像源,例如ubuntu 每个镜像都有 ...

  3. KVM管理平台openebula安装

    1.1opennebula控制台的安装 (如果要添加映像需要给200G以上给/var/lib/one,本文是共享/var/lib/one实现监控,用映像出创建虚拟机原理是从opennebula控制平台 ...

  4. docker实战——在测试中使用Docker

    在之前几章中介绍的都是Docker的基础知识,了解什么是镜像,docker基本的启动流程,以及如何去运作一个容器等等. 接下来的几个章节将介绍如何在实际开发和测试过程中使用docker. 将Docke ...

  5. Docker 创建image

      images 是containers的基础.每次使用docker run 命令都要指定image.   列出本地images   zane@zane-V:~$ docker images REPO ...

  6. 关系型数据库与NoSQL的对比

    SQL(结构化的查询语言)数据库是过去四十年间存储数据的主要方式.20世纪90年代末随着Web应用和MySQL.PostgreSQL和SQLite等开源数据库的兴起,用户爆炸式的增长. NoSQL数据 ...

  7. [Sinatra、Mongo] Mongo

    Mongo is a document-oriented database. Install the required gems: gem install mongo gem install bson ...

  8. Sinatra+SQLite3+DataMapper - 十分完整的tutorial - “Superdo”

    原文地址:https://ididitmyway.herokuapp.com/past/2010/3/30/superdo_a_sinatra_and_datamapper_to_do_list/ 这 ...

  9. [sinatra] Sinatra再入门

    原文URL:http://www.rubycc.com/bbs/topic_detail/86 1.基础代码app.rb require 'rubygems' require 'sinatra/bas ...

随机推荐

  1. [LeetCode] Edit Distance(很好的DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  2. HTML-002-弹出对话框

    日常的网页编程中,弹出对话框经常会以各种形式出现,例如:信息提示框.确认框.新增.修改信息等对话框均是其不同的表现形式. 此文以弹出信息新增对话框进行简要演示,经请参阅! 以下为其对应的结构目录: a ...

  3. imx6 framebuffer 分析

    分析imx6 framebuffer设备和驱动的注册过程. Tony Liu, 2016-8-31, Shenzhen 相关文件: arch/arm/mach-mx6/board-mx6q_sabre ...

  4. SpringMVC自动扫描@Controller注解的bean

    若要对@Controller注解标注的bean进行自动扫描,必须将<context:component-scan base-package="包路径.controller"/ ...

  5. Java: arr==null vs arr.length==0

    当 arr 是一个array时,写Java开始的corner case常常会写类似下面的语句: if(arr == null || arr.length == 0){ return 0; } 其实这是 ...

  6. day01-基础内容

    day01-基础内容 1.Linux:  1)开源的操作系统.免费的    主要用于服务器端,而Java主要是服务器端开发  2)Linux与Windows目录结构的区别:    2.1)文件系统不同 ...

  7. SQL Server 2008 R2 数据库安装

    操作系统    Windows server 2008 R2 数据库      SQL Server 2008 R2 注意:SQL Server 2008 R2需要操作系统首先安装.NET Frame ...

  8. windows 64位 dll文件 位置及python包rtree shapely安装

    位置 \Windows\System32 python包依赖包安装 rtree 依赖 spatialindex(spatialindex.dll   spatialindex_c.dll) shape ...

  9. 解决: libcimtd.lib not found, rpcndr.lib not found

    在编译Inside COM这本书的代码的时候. 报这个错. 毕竟1996年的代码... 原因很简单: libcimtd.lib 是 VC6时代的东西(对应着iostream.h)...现在的MS编译器 ...

  10. 转:DLL如何导出C++的类

    由于DLL的出现是针对C语言的,本身对C++的支持不够好.所以如何从DLL中导出C++的类作为DLL的API的一部分就成了问题. 我试了一下 class __declspec(dllexport) F ...