[sinatra] Just Do It: Learn Sinatra, Part One Darren Jones
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的更多相关文章
- 【sinatra】安装测试
$ gem install sinatra 测试: $ subl app.rb app.rb内容: require 'sinatra' get '/' do "Hello, World!&q ...
- docker镜像的操作
在主机上列出镜像 sudo docker images 每从Docker Hub下载一个镜像就会启动相对的创建一个容器 在镜像列表中看到三个重要的东西: 来自什么镜像源,例如ubuntu 每个镜像都有 ...
- KVM管理平台openebula安装
1.1opennebula控制台的安装 (如果要添加映像需要给200G以上给/var/lib/one,本文是共享/var/lib/one实现监控,用映像出创建虚拟机原理是从opennebula控制平台 ...
- docker实战——在测试中使用Docker
在之前几章中介绍的都是Docker的基础知识,了解什么是镜像,docker基本的启动流程,以及如何去运作一个容器等等. 接下来的几个章节将介绍如何在实际开发和测试过程中使用docker. 将Docke ...
- Docker 创建image
images 是containers的基础.每次使用docker run 命令都要指定image. 列出本地images zane@zane-V:~$ docker images REPO ...
- 关系型数据库与NoSQL的对比
SQL(结构化的查询语言)数据库是过去四十年间存储数据的主要方式.20世纪90年代末随着Web应用和MySQL.PostgreSQL和SQLite等开源数据库的兴起,用户爆炸式的增长. NoSQL数据 ...
- [Sinatra、Mongo] Mongo
Mongo is a document-oriented database. Install the required gems: gem install mongo gem install bson ...
- Sinatra+SQLite3+DataMapper - 十分完整的tutorial - “Superdo”
原文地址:https://ididitmyway.herokuapp.com/past/2010/3/30/superdo_a_sinatra_and_datamapper_to_do_list/ 这 ...
- [sinatra] Sinatra再入门
原文URL:http://www.rubycc.com/bbs/topic_detail/86 1.基础代码app.rb require 'rubygems' require 'sinatra/bas ...
随机推荐
- Tram---poj1847(简单最短路)
题目链接:http://poj.org/problem?id=1847 题意:给了N个交叉口,每个交叉口有自己能转到的交叉口. 注意这里:First number in the i-th line, ...
- SQL server 2012
MICROSOFT SQL SERVER 2012 企业核心版激活码序列号: FH666-Y346V-7XFQ3-V69JM-RHW28MICROSOFT SQL SERVER 2012 商业智能版激 ...
- LintCode A + B Problem
原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/ 不让用 数学运算符,就用位运算符. a的对应位 ^ b的对应位 ^ carry 就是re ...
- How to read the HTML DTD
Contents How to read the HTML DTD 1. DTD Comments 2. Parameter Entity definitions 3. Element declara ...
- C#资源释放
转自:http://www.cnblogs.com/psunny/archive/2009/07/07/1518812.html 深刻理解C#中资源释放 今天我的一个朋友看到我写的那篇<C#中用 ...
- [BS-19]更改UITextField的placeholder文字颜色的5种方法
更改UITextField的placeholder文字颜色的5种方法 想要达到的目标是:一个页面上有多个UITextField,当用户聚焦某textField时,该文本框的placeholder的文字 ...
- javascript设计模式学习之九——命令模式
一.命令模式使用场景及定义 命令模式常见的使用场景是:有时候需要向某些对象发送请求,但是并不知道请求的接受者是谁,也不知道请求的具体操作是什么.此时希望用一种松耦合的方式来设计程序,使得请求的发送者和 ...
- Map的基本用法(Java)
package home.collection.arr; import java.awt.Window.Type; import java.util.ArrayList; import java.ut ...
- yum只下载安装需要的rpm包
1.安装yum的插件yum-downloadonly yum -y install yum-downloadonly 2.使用 yum -y install somepackges --downloa ...
- bootstrap ace treeview树表
html部分 <div class="widget-main padding-8" style="height:400px;overflow-y: scroll;& ...