Skip to content

Instantly share code, notes, and snippets.

@pobo380
Created May 23, 2012 07:22
Show Gist options
  • Save pobo380/2773673 to your computer and use it in GitHub Desktop.
Save pobo380/2773673 to your computer and use it in GitHub Desktop.
#3 トップページに記事を表示するように
# coding: utf-8
require 'rubygems'
require 'sinatra'
require 'sequel'
## DBへの接続
Sequel::Model.plugin(:schema)
DB = Sequel.sqlite('database.sqlite3')
## テーブルの定義
class Post < Sequel::Model
unless table_exists?
set_schema do
primary_key :id
string :title
string :text, :text => true # これがないと255文字まで
end
create_table
end
end
## トップページ
get '/' do
@posts = Post.all
erb :index
end
## 投稿ページ
get '/new' do
erb :new
end
## 投稿データの受取り
post '/posted' do
Post.create(:title => params[:title], :text => params[:text])
redirect '/'
end
__END__
@@ layout
<html>
<head>
<title>My Blog</title>
</head>
<body>
<%= yield %>
</body>
</html>
@@ index
<h1>pobo380 on a leash.</h1>
<p>私のブログです</p>
<h2>記事</h2>
<hr>
<% @posts.each do |post| %>
<h3><%= post.title %></h3>
<p><%= post.text %></p>
<hr>
<% end %>
@@ new
<h2>新しい記事を投稿</h2>
<form action="/posted" method="post">
<div>title : <input name="title" type="text"></div>
<div>text : <textarea name="text" cols="80" rows="20"></textarea></div>
<input type="submit" value="投稿する">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment