template engine ERB

There are tons of different templating languages out there that we could use to create dynamic webpages, but the one we’re going to work with is called ERB. ERB, Embedded Ruby, is the most commonly used templating language with Ruby web frameworks like Sinatra or Ruby on Rails.

The key concept of ERB is simple: it allows you to write HTML with bits of Ruby code interspersed with regular HTML. Before the template is sent back to the user, it’s compiled. This means that any Ruby code in the template is executed, and all we’re left with is regular HTML.

Brackets are used to wrap the embedded Ruby code, to distinguish it from the HTML.
<% %>

We can also use ERB to insert dynamic data into templates. To do that we need to use a slightly different syntax:
<%= %>

views/home.erb

<% 5.times do %>
  <h1>Today's date is <%= Time.now %></h1>
<% end %>

app.ruby

require "sinatra"

get "/" do
   erb :home
end
vagrant@localhost ruby2]$ ruby app.rb -o 192.168.33.10
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Puma
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://192.168.33.10:4567
Use Ctrl-C to stop

e

extreamly easy way!

rendering HTML

require "sinatra"

get "/" do
  "<h1>Welcome to the <em>homepage</em></h1>"
end

get "/sinatra" do
  "<h3>I love <a href='http://www.sinatrarb.com/'>Sinatra!</a></h3>"
end

get "/square/:num" do
  num = params[:num].to_i
  num * num
end

b

require "sinatra"

get "/cat" do
   send_file "cat.html"
end

start running Sinatra

require "sinatra"
get "/" do
  puts "you requested the root path"
end

Here we go!

require "sinatra"
get "/" do
  "you requested the root path"
end

get "/about" do
  "this app is our first Sinatra App!"
end

get "/roll-die" do
  "Your die roll is...#{rand(1...7)}"
end

a

use variable in routing

require "sinatra"
get "/r/SUBREDDIT_NAME/POST_ID/POST_TITLE" do
  "this is a post"
end

getting a patter with post

require "sinatra"

get "/greet/:name" do
  "Hello there #{params[:name]}!"
end

Think about capitalize

get "/greet/:name" do
  name = params[:name].capitalize
  "Hello there #{name}!"
end

try out this

get "/square/:num" do
  num = params[:num].to_i
  num * num
end

typical sinatra

get '/about' do
	send_file 'about.html'
end

Gemfile

source 'https://rubygems.org'

gem 'sinatra'

gem install sinatra
gem install sinatra-contrib

[vagrant@localhost webapp]$ gem install sinatra
Successfully installed sinatra-1.4.7
Parsing documentation for sinatra-1.4.7
Done installing documentation for sinatra after 0 seconds
1 gem installed

let’s try this

[vagrant@localhost ruby2]$ ruby app.ruby -o 192.168.33.10
ruby: No such file or directory -- app.ruby (LoadError)
[vagrant@localhost ruby2]$ ruby app.rb -o 192.168.33.10
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Puma
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://192.168.33.10:4567
Use Ctrl-C to stop
192.168.33.1 - - [14/Jan/2017:08:54:48 +0900] "GET / HTTP/1.1" 404 466 0.0379
192.168.33.1 - - [14/Jan/2017:08:54:48 +0900] "GET /__sinatra__/404.png HTTP/1.1" 200 18893 0.0065
192.168.33.1 - - [14/Jan/2017:08:54:48 +0900] "GET /favicon.ico HTTP/1.1" 404 477 0.0007
^C== Sinatra has ended his set (crowd applauds)
- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2017-01-14 08:55:18 +0900 ===
- Goodbye!

無題

verb path do
  #do something!
end

here is a example

post '/login' do
  #do something!
end