LoginSignup
21
19

More than 5 years have passed since last update.

sinatra入門

Last updated at Posted at 2014-09-29

====================

目的

sinatraでwebアプリをつくる

ソース

前提

ソフトウェア バージョン 備考
OS X 10.8.5
ruby 2.0.0-p247
rvm 1.24.0
sinatra 1.4.4
thin 1.6.0

構成

詳細

セットアップ

$ rvm use ruby-2.0.0-p247
$ rvm gemset create sinatra
$ rvm use ruby-2.0.0-p247@sinatra
$ gem install bundler
$ bundle init
$ bundle

アプリケーションサンプル1

プログラム作成

  • server.rb

    require 'sinatra'
    
    get '/' do
     "Hello, world!"
    end
    

プログラム実行

$ ruby sample1/server.rb

プログラム確認

http://localhost:4567/

アプリケーションサンプル2

プログラム作成

プログラム実行

$ ruby sample2/game.rb

プログラム確認

http://localhost:4567/throw/scissors

アプリケーションサンプル3

プログラム作成

プログラム実行

$ ruby sample3/inline.rb

プログラム確認

http://localhost:4567/index

アプリケーションサンプル4

プログラム作成

sample4
├── app.rb
└── views
    ├── index.erb
    └── user
        ├── help.erb
        └── profile.erb

プログラム実行

$ ruby sample4/add.rb

プログラム確認

http://localhost:4567/index

アプリケーションサンプル5

プログラム作成

sample5
├── config.ru
├── controllers
│   ├── application_controller.rb
│   └── example_controller.rb
├── helpers
│   └── application_helper.rb
└── views
    ├── example.erb
    ├── layout.erb
    └── not_found.erb

プログラム実行

$ cd sample5
$ rackup -p 4567 -s thin

プログラム確認

http://localhost:4567/

参照

Sinatra

入門 Sinatra――Web制作のためのシンプルなRuby DSL

第23回 Rackとは何か(1)Rackの生まれた背景

第24回 Rackとは何か(2)Rackの使い方

第25回 Rackとは何か(3)ミドルウェアのすすめ

21
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
19