[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 ...
随机推荐
- 1006 最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...
- Round and Round We Go
http://acm.hdu.edu.cn/showproblem.php?pid=1313 考查大整数与小整数相乘 #include<iostream> #include<cstd ...
- windows 访问 ubuntu虚拟机 django服务器 失败
配置ubuntu配置成桥接,在ubuntu虚拟机中运行django.py开发服务器.windows访问django失败. 虚拟机运行: python manage.py runserver 0.0.0 ...
- SQL、LINQ、Lambda 三种用法(转)
SQL.LINQ.Lambda 三种用法颜色注释: SQL LinqToSql Lambda QA1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname, ...
- python笔记 - day8
python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...
- git安装步骤
Git简单使用说明 安装git msysgit是Windows版的Git,从https://git-for-windows.github.io下载,然后按默认选项安装即可(注:安装适合自己电脑版本的g ...
- onRetainNonConfigurationInstance和getLastNonConfigurationInstance
很多网友可能知道Android横竖屏切换时会触发onSaveInstanceState,而还原时会产生 onRestoreInstanceState,但是Android的Activity类还有一个方法 ...
- iptables调试方法
iptables调试时,使用到raw表.ipt_LOG内核模块.日志记录在kern.log中. 具体的步骤如下: 1.准备ipt_LOG内核模块 modprobe ipt_LOG 2.使用raw表,加 ...
- myeclipse中的web项目导入到eclipse中注意事项,项目部署到tomcat后无法访问jsp文件
打开eclipse,点击空白处,右键可以看到import>general>existing projects into workspace>next>选择你的myeclipse ...
- Speed-BI 云平台视频观看频道
数据分析的关键,首先是要有数据进行透视分析.大家一般在使用EXCEL透视表进行数据分析时,会通过某个系统,导出类似视频中的数据底稿,然后在此基础上进行各种维度的变换与指标的改变.奥威思必得也有一个类似 ...