Skip to content

Instantly share code, notes, and snippets.

@pobo380
Created May 30, 2012 06:56
Show Gist options
  • Save pobo380/2834171 to your computer and use it in GitHub Desktop.
Save pobo380/2834171 to your computer and use it in GitHub Desktop.
#8-2 管理画面の追加 (記事投稿を管理画面内へ)
# 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
class Comment < Sequel::Model
unless table_exists?
set_schema do
primary_key :id
foreign_key :post_id, :posts
string :text, :text => true
datetime :created_at
end
create_table
end
end
## トップページ
get '/' do
@posts = Post.all
erb :index
end
## 記事の個別ページ
get '/article/:id' do
@post = Post.find(:id => params[:id])
@comments = Comment.filter(:post_id => params[:id]).all
erb :article
end
## コメントデータの受取り
post '/article/:id/commented' do
@comment = Comment.create(:post_id => params[:id], :text => params[:text], :created_at => DateTime.new)
redirect "/article/#{params[:id]}"
end
## 管理画面
get '/admin' do
@posts = Post.all
erb :admin
end
## 投稿ページ
get '/admin/new' do
erb :new
end
## 投稿データの受取り
post '/admin/posted' do
Post.create(:title => params[:title], :text => params[:text], :created_at => DateTime.now)
redirect '/admin'
end
## 編集画面
get '/admin/article/:id/edit' do
@post = Post.find(:id => params[:id])
erb :edit
end
post '/admin/article/:id/edit' do
post = Post.find(:id => params[:id])
post.title = params[:title]
post.text = params[:text]
post.save_changes
redirect "/article/#{params[:id]}"
end
## 削除
get '/admin/article/:id/delete' do
post = Post.find(:id => params[:id])
post.delete
redirect "/admin"
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="/admin">管理ページ</a>
@@ new
<h2>新しい記事を投稿</h2>
<form action="/admin/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>
<h2>コメント</h2>
<% @comments.each do |comment| %>
<p><%= comment.text %></p>
<% end %>
<form action="./<%= params[:id] %>/commented" method="post">
<div>text : <textarea name="text" cols="40" rows="6"></textarea></div>
<input type="submit" value="コメントする">
</form>
@@ admin
<h1>管理ページ</h1>
<div><a href="/admin/new">新しい記事を投稿する</a></div>
<% @posts.each do |post| %>
<div>
<%=post.id%>. <strong><%=post.title %></strong>
<a href="/admin/article/<%=post.id%>/edit">edit</a>
<a href="/admin/article/<%=post.id%>/delete">delete</a>
</div>
<% end %>
@@ edit
<h1>記事を編集する</h1>
<form action="/admin/article/<%=@post.id%>/edit" method="post">
<div>title : <input name="title" type="text" value="<%=@post.title%>"></div>
<div>text : <textarea name="text" cols="80" rows="20"><%=@post.text%></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