-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
169 lines (119 loc) · 3.15 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'bundler'
Bundler.require
require 'rack'
require 'sinatra/reloader'
require 'rack-flash'
set :bind, '0.0.0.0'
# set :environment, :production
# connectionのところのdevelopmentはシンボルで
ActiveRecord::Base.configurations=YAML.load_file('db/database.yml')
ActiveRecord::Base.establish_connection(:development)
class Line
class User < ActiveRecord::Base
has_many :Comments
# passwordをハッシュ化→bcrypt gemを読み込む
has_secure_password
# これはDB上でnot nullにしているのでいらないかも
validates :name, :password, :email, presence: true
validates :password, :email, length: { minimum: 6 }
end
class Comment < ActiveRecord::Base
#usersとリレーション
belongs_to :user
end
#セッション
enable :sessions
# Flashが使用可
use Rack::Flash
before do
set_login_user
set_to_user
end
helpers Sinatra::ContentFor
helpers do
def login?
session[:user_id].present?
end
def set_login_user
@login_user = User.find_by(id: session[:user_id]) if login?
end
def exist?
params[:id].present?
end
def set_to_user
@to_user_id = Comment.find_by(to_user: params[:id]) if exist?
end
# エスケープ処理
include Rack::Utils
alias_method :h, :escape_html
end
#-----------------------------------------------------------------
get '/' do
@title ='index_page'
erb :index_page
end
get '/login' do
@title = "login_page"
erb :login_page
end
post '/login' do
user = User.find_by(name: params[:name])
#userの名前とパスワードの一致させる
if user && user.authenticate(params[:password])
session[:user_id] = user.id
flash[:notice] = "ログインに成功しました。"
redirect "/home"
else
flash[:notice] = "ログインしてください"
erb :login_page
end
end
get '/home' do
@users = User.order("id")
erb :home
end
get '/my_page' do
end
get '/talk/:id/:name' do
@to_user_id = params[:id]
@to_user = params[:name]
@comments = Comment.where(user_id: session[:user_id], to_user: @to_user_id).all
# @comments = Comment.find_by(user_id: session[:id], to_user: params[:id])
erb :talk
end
post '/new' do
if params[:body].present?
@comments = Comment.create( user_id: session[:user_id], body: params[:body], to_user: params[:id])
redirect back
end
end
post '/login_failure' do
@title = "login_failure"
"ログインに失敗しました"
redirect '/login'
end
get '/create_account' do
erb :create_account
end
post '/create_account' do
@title = "create_account"
user = User.new do |u|
u.name = params[:name]
u.password = params[:password]
u.email = params[:email]
u.save
end
# userが有効で保存されているなら
if user.valid? && user.save
session[:user_id] = user.id
session[:user_name] = user.name
redirect '/home'
else
redirect back
end
end
get '/logout' do
session.clear
end
# HelloWorldApp.run! if app_file == $0
end