質問編集履歴

1

追記しました

2021/02/23 14:50

投稿

haru24s
haru24s

スコア4

test CHANGED
File without changes
test CHANGED
@@ -121,3 +121,269 @@
121
121
  end
122
122
 
123
123
  ```
124
+
125
+ ```
126
+
127
+ /models/user.rb
128
+
129
+
130
+
131
+ class User < ApplicationRecord
132
+
133
+ has_secure_password
134
+
135
+
136
+
137
+ validates :name, {presence: true}
138
+
139
+ validates :email, {presence: true, uniqueness: true}
140
+
141
+ validates :password, {presence: true}
142
+
143
+ end
144
+
145
+ ```
146
+
147
+ ```
148
+
149
+ /user_controller.rb
150
+
151
+
152
+
153
+ class UsersController < ApplicationController
154
+
155
+ before_action :authenticate_user, {only: [:index, :show, :edit, :update]}
156
+
157
+ before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]}
158
+
159
+ before_action :ensure_correct_user, {only: [:edit, :update]}
160
+
161
+
162
+
163
+ def index
164
+
165
+ @users = User.all
166
+
167
+ end
168
+
169
+
170
+
171
+ def show
172
+
173
+ @user = User.find_by(id:params[:id])
174
+
175
+ end
176
+
177
+
178
+
179
+ def new
180
+
181
+ @user = User.new
182
+
183
+ end
184
+
185
+
186
+
187
+ def create
188
+
189
+ @user = User.new(user_params)
190
+
191
+ if @user.save
192
+
193
+ session[:user_id] = @user.id
194
+
195
+ flash[:notice]="ユーザー登録が完了しました"
196
+
197
+ redirect_to("/users/#{@user.id}")
198
+
199
+ else
200
+
201
+ render("users/new")
202
+
203
+ end
204
+
205
+
206
+
207
+ end
208
+
209
+
210
+
211
+ def edit
212
+
213
+ @user = User.find_by(id: params[:id])
214
+
215
+ end
216
+
217
+
218
+
219
+ def update
220
+
221
+ @user = User.find_by(id: params[:id])
222
+
223
+
224
+
225
+ if @user.update(user_params)
226
+
227
+ flash[:notice] = "ユーザー情報を編集しました"
228
+
229
+ redirect_to("/users/#{@user.id}")
230
+
231
+ else
232
+
233
+ render("users/edit")
234
+
235
+ end
236
+
237
+ end
238
+
239
+
240
+
241
+ def login_form
242
+
243
+
244
+
245
+ end
246
+
247
+
248
+
249
+ def login
250
+
251
+ @user = User.find_by(email: params[:email])
252
+
253
+ if @user && @user.authenticate(params[:password])
254
+
255
+ session[:user_id] = @user.id
256
+
257
+ flash[:notice] = "ログインしました"
258
+
259
+ redirect_to("/posts")
260
+
261
+ else
262
+
263
+ @error_message = "メールアドレスまたはパスワードが間違っています"
264
+
265
+ @email = params[:email]
266
+
267
+ @password = params[:password]
268
+
269
+ render("users/login_form")
270
+
271
+ end
272
+
273
+ end
274
+
275
+
276
+
277
+ def logout
278
+
279
+ session[:user_id] = nil
280
+
281
+ flash[:notice] ="ログアウトしました"
282
+
283
+ redirect_to("/login")
284
+
285
+ end
286
+
287
+
288
+
289
+ def ensure_correct_user
290
+
291
+ if @current_user.id != params[:id].to_i
292
+
293
+ flash[:notice] = "権限がありません"
294
+
295
+ redirect_to("/posts")
296
+
297
+ end
298
+
299
+ end
300
+
301
+
302
+
303
+ def update_name_and_email
304
+
305
+ @user = User.find_by(id: params[:id])
306
+
307
+
308
+
309
+ if @user.update_attributes(name: params[:name], email: params[:email])
310
+
311
+ flash[:notice] = "ユーザー情報を編集しました"
312
+
313
+ redirect_to("/users/#{@user.id}")
314
+
315
+ else
316
+
317
+ render("users/edit")
318
+
319
+ end
320
+
321
+ end
322
+
323
+
324
+
325
+
326
+
327
+ private
328
+
329
+
330
+
331
+ def user_params
332
+
333
+ params.require(:user).permit(:name, :email, :password)
334
+
335
+ end
336
+
337
+
338
+
339
+ end
340
+
341
+ ```
342
+
343
+ ```
344
+
345
+ schema.rb
346
+
347
+
348
+
349
+ ActiveRecord::Schema.define(version: 2021_02_14_124745) do
350
+
351
+
352
+
353
+ create_table "posts", force: :cascade do |t|
354
+
355
+ t.string "title"
356
+
357
+ t.text "content"
358
+
359
+ t.integer "user_id"
360
+
361
+ t.datetime "created_at", null: false
362
+
363
+ t.datetime "updated_at", null: false
364
+
365
+ t.string "img"
366
+
367
+ end
368
+
369
+
370
+
371
+ create_table "users", force: :cascade do |t|
372
+
373
+ t.string "name"
374
+
375
+ t.string "email"
376
+
377
+ t.datetime "created_at", null: false
378
+
379
+ t.datetime "updated_at", null: false
380
+
381
+ t.string "password_digest"
382
+
383
+ end
384
+
385
+
386
+
387
+ end
388
+
389
+ ```