Skip to content

Instantly share code, notes, and snippets.

@Momozono
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Momozono/f8e25270cc71c0ce659f to your computer and use it in GitHub Desktop.
Save Momozono/f8e25270cc71c0ce659f to your computer and use it in GitHub Desktop.
#-*-coding: utf-8-*-
require 'active_record'
require 'sinatra'
require 'sinatra/reloader'
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: "list.db"
)
class User < ActiveRecord::Base
end
get '/' do
@users = User.all
erb :index
end
post '/new' do
user = User.new
user.id = params[:id]
user.name = params[:name]
user.age = params[:age]
user.email = params[:email]
user.save
redirect '/'
end
delete '/del' do
user = User.find(params[:id])
user.destroy
redirect '/'
end
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body>
<table border>
<tr>
<th>ID</th>
<th>名前</th>
<th>年齢</th>
<th>Email</th>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.name %></td>
<td><%= user.age %></td>
<td><%= user.email %></td>
<form method='POST' action='del'>
<td><input type='submit' value='削除'></td>
<input type='hidden' name='id' value="<%= user.id %>">
<input type='hidden' name='_method' value='delete'>
</form>
</tr>
<% end %>
<form method='POST' action='new'>
<tr>
<td><input type='text' name='id'></td>
<td><input type='text' name='name'></td>
<td><input type='text' name='age'></td>
<td><input type='text' name='email'></td>
<td><input type='submit' name='登録'></td>
</tr>
</form>
</table>
<div class='field'></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment