teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

9

posts_controller.rbの中身を記載しました。

2019/07/01 03:38

投稿

oku-haru
oku-haru

スコア10

title CHANGED
File without changes
body CHANGED
@@ -288,7 +288,72 @@
288
288
  Rendered users/login_form.html.erb within layouts/application (29.9ms)
289
289
  Completed 200 OK in 386ms (Views: 363.3ms | ActiveRecord: 0.9ms)
290
290
  ```
291
+ posts_controller.rb
292
+ ```
293
+ class PostsController < ApplicationController
294
+ before_action :authenticate_user
295
+ before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
296
+
297
+ def index
298
+ @posts = Post.all.order(created_at: :desc)
299
+ end
300
+
301
+ def show
302
+ @post = Post.find_by(id: params[:id])
303
+ @user = @post.user
304
+ @likes_count = Like.where(post_id: @post.id).count
305
+ end
306
+
307
+ def new
308
+ @post = Post.new
309
+ end
310
+
311
+ def create
312
+ @post = Post.new(
313
+ content: params[:content],
314
+ user_id: @current_user.id
315
+ )
316
+ if @post.save
317
+ flash[:notice] = "投稿を作成しました"
318
+ redirect_to("/posts/index")
319
+ else
320
+ render("posts/new")
321
+ end
322
+ end
323
+
324
+ def edit
325
+ @post = Post.find_by(id: params[:id])
326
+ end
327
+
328
+ def update
329
+ @post = Post.find_by(id: params[:id])
330
+ @post.content = params[:content]
331
+ if @post.save
332
+ flash[:notice] = "投稿を編集しました"
333
+ redirect_to("/posts/index")
334
+ else
335
+ render("posts/edit")
336
+ end
337
+ end
338
+
339
+ def destroy
340
+ @post = Post.find_by(id: params[:id])
341
+ @post.destroy
342
+ flash[:notice] = "投稿を削除しました"
343
+ redirect_to("/posts/index")
344
+ end
345
+
346
+ def ensure_correct_user
347
+ @post = Post.find_by(id: params[:id])
348
+ if @post.user_id != @current_user.id
349
+ flash[:notice] = "権限がありません"
350
+ redirect_to("/posts/index")
351
+ end
352
+ end
353
+
354
+ end
291
355
 
356
+ ```
292
357
 
293
358
 
294
359
  ### 試したこと

8

ログイン時のログを記入

2019/07/01 03:38

投稿

oku-haru
oku-haru

スコア10

title CHANGED
File without changes
body CHANGED
@@ -259,6 +259,38 @@
259
259
  </html>
260
260
  ```
261
261
 
262
+ ログイン時のlogを貼ってみました。
263
+
264
+ ```
265
+ Started POST "/login" for ::1 at 2019-07-01 08:07:10 +0900
266
+ Processing by UsersController#login as HTML
267
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"2XerjpX7waxUGUlglL3DAa6aqksVlJ/A4HUelqF9833/UqIjdWSSg1x6LkCXGfQzII7nDLRnLQXdWwQQfXcXIQ==", "email"=>"test-user.@gmail.com", "password"=>"[FILTERED]"}
268
+ User Load (11.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
269
+ User Load (29.7ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test-user@gmail.com' LIMIT 1
270
+ Redirected to http://localhost:3000/posts/index?containerPort=3000&languageName=rails5&locale=ja
271
+ Completed 302 Found in 534ms (ActiveRecord: 41.0ms)
272
+
273
+
274
+ Started GET "/posts/index?containerPort=3000&languageName=rails5&locale=ja" for ::1 at 2019-07-01 08:07:10 +0900
275
+ Processing by PostsController#index as HTML
276
+ Parameters: {"containerPort"=>"3000", "languageName"=>"rails5", "locale"=>"ja"}
277
+ User Load (4.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
278
+ Redirected to http://localhost:3000/login?containerPort=3000&languageName=rails5&locale=ja
279
+ Filter chain halted as :authenticate_user rendered or redirected
280
+ Completed 302 Found in 30ms (ActiveRecord: 4.0ms)
281
+
282
+
283
+ Started GET "/login?containerPort=3000&languageName=rails5&locale=ja" for ::1 at 2019-07-01 08:07:10 +0900
284
+ Processing by UsersController#login_form as HTML
285
+ Parameters: {"containerPort"=>"3000", "languageName"=>"rails5", "locale"=>"ja"}
286
+ User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
287
+ Rendering users/login_form.html.erb within layouts/application
288
+ Rendered users/login_form.html.erb within layouts/application (29.9ms)
289
+ Completed 200 OK in 386ms (Views: 363.3ms | ActiveRecord: 0.9ms)
290
+ ```
291
+
292
+
293
+
262
294
  ### 試したこと
263
295
 
264
296
  DBがおかしいのか?と考え

7

タイトルの変更

2019/06/30 23:12

投稿

oku-haru
oku-haru

スコア10

title CHANGED
@@ -1,1 +1,1 @@
1
- Ruby on Railsで ログインページからログイン後、投稿ページへリダイレトさせたい。
1
+ Ruby on Railsで /signup → /users/ , /login → /posts/index へリを飛ばしたい。
body CHANGED
@@ -14,7 +14,10 @@
14
14
 
15
15
  こういったURLになり
16
16
  ログインページ(テキストエリア)のままになります。
17
+ 本来ですと、下記のように
18
+ localhost:3000/login → localhost:3000/posts/index
17
19
 
20
+ なるはずなのですが・・・
18
21
  多分何かが足りない?のか、何かが間違っているのか
19
22
  よくわかりません。
20
23
 

6

データベースについての自分の意見を加えました。

2019/06/29 18:15

投稿

oku-haru
oku-haru

スコア10

title CHANGED
File without changes
body CHANGED
@@ -258,13 +258,18 @@
258
258
 
259
259
  ### 試したこと
260
260
 
261
+ DBがおかしいのか?と考え
262
+ rake db:migrate:reset
261
- ユーザの新規登録の確認
263
+ を実行し、再度新規登録画面から開始
262
- DBにもユーザが登録されていること確認できているので
264
+ DB新規登録id,passが登録されていること確認できています。
263
- DBの問題ではないと思っています。
264
265
 
265
266
  ログイン後の挙動
266
- redirect_toがおかしいのかな、と思っています。
267
+ redirect_toの動きがおかしいのか
268
+ current_userの動きがおかしいのかな?
269
+ と思い色々見ていますが
267
270
 
271
+ 何が正しくて何が間違っているのか
272
+ よくわからなくなりました・・・
268
273
 
269
274
  ### 補足情報(FW/ツールのバージョンなど)
270
275
  rails -v '5.0.3'

5

コードを見やすくコードタイトルの位置を変更しました。

2019/06/29 18:11

投稿

oku-haru
oku-haru

スコア10

title CHANGED
File without changes
body CHANGED
@@ -34,8 +34,8 @@
34
34
 
35
35
  ### 該当のソースコード
36
36
 
37
+ route.rb
37
38
  ```
38
- route.rb
39
39
 
40
40
  Rails.application.routes.draw do
41
41
 

4

application.html.erb application_controller.rbを新たに加えました。

2019/06/29 18:07

投稿

oku-haru
oku-haru

スコア10

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  現在はログイン後
12
12
  localhost:3000/login →
13
- http://localhost:3000/login?containerPort=3000&languageName=rails5&locale=ja
13
+ localhost:3000/login?containerPort=3000&languageName=rails5&locale=ja
14
14
 
15
15
  こういったURLになり
16
16
  ログインページ(テキストエリア)のままになります。
@@ -21,7 +21,14 @@
21
21
  routeとuserのコントローラーがおかしいのかと思って
22
22
  みているのですが、よくわかりません。
23
23
 
24
+ route.rb
25
+ users_controller.rb
26
+ application.html.erb
27
+ application_controller.rb
28
+
24
- routeとコントローラーの内容を載せています
29
+ の内容を載せています
30
+ current_userの動きがおかしいでしょうか・・・?
31
+
25
32
  ここなのでは?というのがあれば、すぐ載せますので
26
33
  連絡お願いします。
27
34
 
@@ -59,7 +66,7 @@
59
66
  end
60
67
 
61
68
  ```
62
- usercontroller
69
+ users_controller.rb
63
70
  ```
64
71
 
65
72
  class UsersController < ApplicationController
@@ -159,7 +166,96 @@
159
166
  end
160
167
 
161
168
  ```
169
+ application_controller.rb
170
+ ```
171
+ class ApplicationController < ActionController::Base
172
+ before_action :set_current_user
173
+
174
+ def set_current_user
175
+ @current_user = User.find_by(id: session[:user_id])
176
+ end
177
+
178
+ def authenticate_user
179
+ if @current_user == nil
180
+ flash[:notice] = "ログインが必要です"
181
+ redirect_to("/login")
182
+ end
183
+ end
184
+
185
+ def forbid_login_user
186
+ if @current_user
187
+ flash[:notice] = "すでにログインしています"
188
+ redirect_to("/posts/index")
189
+ end
190
+ end
162
191
 
192
+ end
193
+ ```
194
+ application.html.erb
195
+ ```
196
+ <!DOCTYPE html>
197
+ <html>
198
+ <head>
199
+ <title>ROMI-X</title>
200
+ <%= csrf_meta_tags %>
201
+
202
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
203
+ <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
204
+
205
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
206
+ </head>
207
+
208
+ <body>
209
+ <header>
210
+ <div class="header-logo">
211
+ <% if @current_user %>
212
+ <%= link_to("LOMI-X", "/posts/index") %>
213
+ <% else %>
214
+ <%= link_to("LOMI-X", "/") %>
215
+ <% end %>
216
+ </div>
217
+ <ul class="header-menus">
218
+ <% if @current_user %>
219
+ <li>
220
+ <%= link_to(@current_user.name, "/users/#{@current_user.id}") %>
221
+ </li>
222
+ <li>
223
+ <%= link_to("投稿一覧", "/posts/index") %>
224
+ </li>
225
+ <li>
226
+ <%= link_to("新規投稿", "/posts/new") %>
227
+ </li>
228
+ <li>
229
+ <%= link_to("ユーザー一覧", "/users/index") %>
230
+ </li>
231
+ <li>
232
+ <%= link_to("ログアウト", "/logout", {method: :post}) %>
233
+ </li>
234
+ <% else %>
235
+ <li>
236
+ <%= link_to("LOMI-Xとは", "/about") %>
237
+ </li>
238
+ <li>
239
+ <%= link_to("新規登録", "/signup") %>
240
+ </li>
241
+ <li>
242
+ <%= link_to("ログイン", "/login") %>
243
+ </li>
244
+ <% end %>
245
+ </ul>
246
+ </header>
247
+
248
+ <% if flash[:notice] %>
249
+ <div class="flash">
250
+ <%= flash[:notice] %>
251
+ </div>
252
+ <% end %>
253
+
254
+ <%= yield %>
255
+ </body>
256
+ </html>
257
+ ```
258
+
163
259
  ### 試したこと
164
260
 
165
261
  ユーザの新規登録の確認

3

飛ばす先を明記しました

2019/06/29 18:04

投稿

oku-haru
oku-haru

スコア10

title CHANGED
File without changes
body CHANGED
@@ -1,13 +1,17 @@
1
1
  ### 前提・実現したいこと
2
2
  Ruby on Railsで
3
- ログインページからログイン後、投稿ページへリダイレクトさせたい。
3
+ ログインページからログイン後、マイページへリダイレクトさせたい。
4
+ localhost:3000/signup → localhost:3000/users/
5
+ localhost:3000/login → localhost:3000/posts/indexへ飛ばしたい。
4
6
 
5
7
  ### 発生している問題・エラーメッセージ
6
8
  エラーメッセージはありませんが
7
- ログインページ(メールアドレス等記入するテキストエリア)からログイン後、投稿ページへリダイレクトできません。
9
+ ログインページ(メールアドレス等記入するテキストエリア)からログイン後、マイページへリダイレクトできません。
8
10
 
9
11
  現在はログイン後
12
+ localhost:3000/login →
10
13
  http://localhost:3000/login?containerPort=3000&languageName=rails5&locale=ja
14
+
11
15
  こういったURLになり
12
16
  ログインページ(テキストエリア)のままになります。
13
17
 

2

2019/06/29 17:22

投稿

oku-haru
oku-haru

スコア10

title CHANGED
File without changes
body CHANGED
@@ -171,9 +171,9 @@
171
171
  ruby -v 2.6.3
172
172
  'mysql2', '~>0.4.1'
173
173
 
174
- ここにより詳細な情報を記載してください。
175
- いわゆる、progaterのど初心者です。
176
174
 
175
+ いわゆる、progaterの、ど初心者です。
176
+
177
177
  progateのruby on railsのコースをほぼそのまま写経して作っています。
178
178
 
179
179
  環境構築等は独学でsqliteからMysqlへと変更しています。

1

2019/06/29 08:49

投稿

oku-haru
oku-haru

スコア10

title CHANGED
File without changes
body CHANGED
@@ -4,12 +4,12 @@
4
4
 
5
5
  ### 発生している問題・エラーメッセージ
6
6
  エラーメッセージはありませんが
7
- ログインページからログイン後、投稿ページへリダイレクトできません。
7
+ ログインページ(メールアドレス等記入するテキストエリア)からログイン後、投稿ページへリダイレクトできません。
8
8
 
9
9
  現在はログイン後
10
10
  http://localhost:3000/login?containerPort=3000&languageName=rails5&locale=ja
11
11
  こういったURLになり
12
- ログインページのままになります。
12
+ ログインページ(テキストエリア)のままになります。
13
13
 
14
14
  多分何かが足りない?のか、何かが間違っているのか
15
15
  よくわかりません。