Skip to content

Instantly share code, notes, and snippets.

@pobo380
Created May 30, 2012 05:54
Show Gist options
  • Save pobo380/2833993 to your computer and use it in GitHub Desktop.
Save pobo380/2833993 to your computer and use it in GitHub Desktop.
#5 投稿日時の追加
# 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文字まで
datetime :created_at
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], :created_at => DateTime.now)
redirect '/'
end
## 記事の個別ページ
get '/article/:id' do
@post = Post.find(:id => params[:id])
erb :article
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>
<% @posts.each do |post| %>
<hr>
<h3><a href="/article/<%=post.id%>"><%= post.title %></a></h3>
<p><%= post.text %></p>
<% end %>
<hr>
<a href="/new">記事を投稿する</a>
@@ 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>
@@ article
<h1><%= @post.title %></h1>
<p><%= @post.created_at %></p>
<p><%= @post.text %></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment