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

質問編集履歴

3

式の修正

2020/05/29 18:24

投稿

tttkkm
tttkkm

スコア10

title CHANGED
File without changes
body CHANGED
@@ -31,13 +31,13 @@
31
31
  ```
32
32
 
33
33
  ```erb
34
- <% posts.each do |post| %>
34
+ <% posts.each do |post| %>
35
-
36
- <%# post_parent_idが0ではない場合、つまり返信コメントの場合の表示 %>
37
-
38
- <% if post.parent_post_id != 0 %>
35
+ <% if post.parent_post_id == 0 %>
36
+ <div class="card">
37
+ <% else %>
39
- <div class="card ml-5">
38
+ <div class="card reply">
39
+ <% end %>
40
- <div class="card-body">
40
+ <div class="card-body post-card">
41
41
  <div class="row align-items-center">
42
42
  <div class="col-1 text-center">
43
43
  <div id="vote-actions-<%= post.id %>" class="vote" data-id="<%=post.id%>">
@@ -50,48 +50,43 @@
50
50
  <small class="card-title">
51
51
  <%= link_to post.user.name , profile_path(post.user.userid) %>
52
52
  <%= post.user.karma %>
53
+ <span class ="float-right"><%= post.created_at.strftime("%Y-%m-%d %H:%M:%S")%></span>
53
54
  </small>
55
+ <div class="card-text">
56
+ <% if post.post_image.present? %>
57
+ <%= image_tag post.post_image.thumb50.url, class: "post-image"%>
58
+ <% end %>
59
+ <br>
54
- <p class="card-text"><%= safe_join(post.content.split("\n"),tag(:br)) %></p>
60
+ <p><%= safe_join(post.content.split("\n"),tag(:br)) %></p>
61
+ </div>
62
+
55
- <% unless current_user.id == post.user_id %>
63
+ <% unless current_user.id == post.user_id || post.parent_id == 0%>
56
- <button type="button" class="btn" data-toggle="modal" data-target="#replyModal" data-parent= <%= post.id %>>
64
+ <button type="button" class="btn reply-btn" data-toggle="modal" data-target="#replyModal" data-parent= <%= post.id %>>
57
- <i class="fas fa-plus"></i>返信する
65
+ <i class="fas fa-plus"></i>返信する
58
- </button>
66
+ </button>
59
- <% end %>
67
+ <% end %>
60
- </div>
61
- </div>
62
- </div>
63
- </div>
64
-
65
- <% else %>
66
68
 
67
- <%# post_parent_idが0の場合、つまり親コメントの場合の表示 %>
68
-
69
- <div class="card">
70
- <div class="card-body">
69
+ <div class="card-info float-right">
70
+
71
+ <% if current_user.id == post.user_id %>
72
+ <%= link_to(edit_post_path(post)) do %>
71
- <div class="row align-items-center">
73
+ <i class="fas fa-pen ml-3"></i>
72
- <div class="col-1 text-center">
74
+ <% end %>
73
- <div id="vote-actions-<%= post.id %>" class="vote" data-id="<%=post.id%>">
75
+ <%= link_to(post, method: :delete, data: { confirm: 'Are you sure?' }) do %>
74
- <div class="fa fa-arrow-up upvote <%= is_upvoted post %> "></div>
76
+ <i class="far fa-trash-alt ml-3"></i>
75
- <span class="font-weight-bold score"><%= post.score%></span>
77
+ <% end %>
76
- <div class="fa fa-arrow-down downvote <%= is_downvoted post %>"></div>
78
+ <% end %>
77
- </div>
79
+ </div>
78
80
  </div>
79
- <div class="col-11">
80
- <small class="card-title">
81
- <%= link_to post.user.name , profile_path(post.user.userid) %>
82
- <%= post.user.karma %>
83
- </small>
84
- <p class="card-text"><%= safe_join(post.content.split("\n"),tag(:br)) %></p>
85
- <% unless current_user.id == post.user_id %>
86
- <button type="button" class="btn" data-toggle="modal" data-target="#replyModal" data-parent= <%= post.id %>>
87
- <i class="fas fa-plus"></i>返信する
88
- </button>
89
- <% end %>
90
- </div>
91
81
  </div>
92
82
  </div>
93
83
  </div>
84
+
94
85
  <% end %>
86
+
87
+
88
+
89
+
95
90
  ```
96
91
 
97
92
 

2

viewの追加

2020/05/29 18:24

投稿

tttkkm
tttkkm

スコア10

title CHANGED
@@ -1,1 +1,1 @@
1
- Railsでの複雑な並び替え
1
+ Railsでの、点数順でのコメントの並び替えと、その下にツリー状で返信を表示させる方法
body CHANGED
@@ -2,8 +2,17 @@
2
2
  お世話になります。
3
3
  現在railsで掲示板サイトを作っています。
4
4
  コメントにユーザーからの投票による点数を設定し、点数順で並び替え、その下にそのコメントへの返信を表示させたいと考えております。
5
- イメージとしてはRedditのようにしたいです。
5
+ イメージとしてはRedditのように
6
6
 
7
+ コメント
8
+   返信
9
+     返信の返信
10
+       ・
11
+       ・
12
+       ・
13
+
14
+ といった形でツリー状に続いていくようにしたいです。
15
+
7
16
  コメントに、parent_post_idというカラムを設け、そこに親コメントのidを挿入しています。親コメントの場合はdefaultで0です。
8
17
 
9
18
  ### 発生している問題・エラーメッセージ
@@ -19,9 +28,73 @@
19
28
  def index
20
29
  @posts= @topic.posts.sort_by{ |p| [p.score, p.parent_post_id] }.reverse
21
30
  end
31
+ ```
22
32
 
33
+ ```erb
34
+ <% posts.each do |post| %>
35
+
36
+ <%# post_parent_idが0ではない場合、つまり返信コメントの場合の表示 %>
37
+
38
+ <% if post.parent_post_id != 0 %>
39
+ <div class="card ml-5">
40
+ <div class="card-body">
41
+ <div class="row align-items-center">
42
+ <div class="col-1 text-center">
43
+ <div id="vote-actions-<%= post.id %>" class="vote" data-id="<%=post.id%>">
44
+ <div class="fa fa-arrow-up upvote <%= is_upvoted post %> "></div>
45
+ <span class="font-weight-bold score"><%= post.score%></span>
46
+ <div class="fa fa-arrow-down downvote <%= is_downvoted post %>"></div>
47
+ </div>
48
+ </div>
49
+ <div class="col-11">
50
+ <small class="card-title">
51
+ <%= link_to post.user.name , profile_path(post.user.userid) %>
52
+ <%= post.user.karma %>
53
+ </small>
54
+ <p class="card-text"><%= safe_join(post.content.split("\n"),tag(:br)) %></p>
55
+ <% unless current_user.id == post.user_id %>
56
+ <button type="button" class="btn" data-toggle="modal" data-target="#replyModal" data-parent= <%= post.id %>>
57
+ <i class="fas fa-plus"></i>返信する
58
+ </button>
59
+ <% end %>
60
+ </div>
61
+ </div>
62
+ </div>
63
+ </div>
64
+
65
+ <% else %>
66
+
67
+ <%# post_parent_idが0の場合、つまり親コメントの場合の表示 %>
68
+
69
+ <div class="card">
70
+ <div class="card-body">
71
+ <div class="row align-items-center">
72
+ <div class="col-1 text-center">
73
+ <div id="vote-actions-<%= post.id %>" class="vote" data-id="<%=post.id%>">
74
+ <div class="fa fa-arrow-up upvote <%= is_upvoted post %> "></div>
75
+ <span class="font-weight-bold score"><%= post.score%></span>
76
+ <div class="fa fa-arrow-down downvote <%= is_downvoted post %>"></div>
77
+ </div>
78
+ </div>
79
+ <div class="col-11">
80
+ <small class="card-title">
81
+ <%= link_to post.user.name , profile_path(post.user.userid) %>
82
+ <%= post.user.karma %>
83
+ </small>
84
+ <p class="card-text"><%= safe_join(post.content.split("\n"),tag(:br)) %></p>
85
+ <% unless current_user.id == post.user_id %>
86
+ <button type="button" class="btn" data-toggle="modal" data-target="#replyModal" data-parent= <%= post.id %>>
87
+ <i class="fas fa-plus"></i>返信する
88
+ </button>
89
+ <% end %>
90
+ </div>
91
+ </div>
92
+ </div>
93
+ </div>
94
+ <% end %>
23
95
  ```
24
96
 
97
+
25
98
  ### 試したこと
26
99
 
27
100
  sort_byでscoreとparent_post_idで並び替えてみた。

1

誤字

2020/05/29 04:33

投稿

tttkkm
tttkkm

スコア10

title CHANGED
File without changes
body CHANGED
@@ -17,7 +17,7 @@
17
17
  ```rb
18
18
  controller
19
19
  def index
20
- @posts= Posts.sort_by{ |p| [p.score, p.parent_post_id] }.reverse
20
+ @posts= @topic.posts.sort_by{ |p| [p.score, p.parent_post_id] }.reverse
21
21
  end
22
22
 
23
23
  ```