Created
May 23, 2012 07:37
-
-
Save pobo380/2773730 to your computer and use it in GitHub Desktop.
#4 リンクを埋め込み
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
## 記事の個別ページ | |
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.text %></p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment