daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Oct 12th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding: utf-8
  2. require 'rubygems'
  3. require 'sinatra'
  4. require 'sequel'
  5.  
  6. ## DBへの接続
  7. Sequel::Model.plugin(:schema)
  8. DB = Sequel.sqlite('database.sqlite3')
  9.  
  10. ## テーブルの定義
  11. class Post < Sequel::Model
  12.   unless table_exists?
  13.     set_schema do
  14.       primary_key :id
  15.       string :title
  16.       string :text, :text => true # これがないと255文字まで
  17.     end
  18.  
  19.     create_table
  20.   end
  21. end
  22.  
  23. ## トップページ
  24. get '/' do
  25.   @posts = Post.all
  26.  
  27.   erb :index
  28. end
  29.  
  30. ## 投稿ページ
  31. get '/new' do
  32.   erb :new
  33. end
  34.  
  35. ## 投稿データの受取り
  36. post '/posted' do
  37.   Post.create(:title => params[:title], :text => params[:text])
  38.  
  39.   redirect '/'
  40. end
  41.  
  42. ## 記事の個別ページ
  43. get '/article/:id' do
  44.   @post = Post.find(:id => params[:id])
  45.  
  46.   erb :article
  47. end
  48.  
  49. __END__
  50.  
  51. @@ layout
  52. <html>
  53.   <head>
  54.   <title>My Blog</title>
  55.   </head>
  56.   <body>
  57.     <%= yield %>
  58.   </body>
  59. </html>
  60.  
  61. @@ index
  62. <h1>pobo380 on a leash.</h1>
  63. <p>私のブログです</p>
  64. <h2>記事</h2>
  65. <hr>
  66. <% @posts.each do |post| %>
  67.   <h3><%= post.title %></h3>
  68.   <p><%= post.text %></p>
  69.   <hr>
  70. <% end %>
  71.  
  72. @@ new
  73. <h2>新しい記事を投稿</h2>
  74. <form action="/posted" method="post">
  75.   <div>title : <input name="title" type="text"></div>
  76.   <div>text : <textarea name="text" cols="80" rows="20"></textarea></div>
  77.   <input type="submit" value="投稿する">
  78. </form>
  79.  
  80. @@ article
  81. <h1><%= @post.title %></h1>
  82. <p><%= @post.text %></p>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top