質問編集履歴
2
ご質問への回答のため編集しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -96,9 +96,75 @@
|
|
96
96
|
|
97
97
|
```
|
98
98
|
|
99
|
-
|
99
|
+
**index.html.erb**
|
100
|
-
|
100
|
+
|
101
|
-
```
|
101
|
+
```
|
102
|
+
|
103
|
+
<div class="album py-5 bg-light mt-3">
|
104
|
+
|
105
|
+
<div class="container">
|
106
|
+
|
107
|
+
<div class="row">
|
108
|
+
|
109
|
+
<% @contents.each do |content| %>
|
110
|
+
|
111
|
+
<div class="col-md-4">
|
112
|
+
|
113
|
+
<div class="card border-dark mb-4 shadow-sm">
|
114
|
+
|
115
|
+
<div class="card-header text-white bg-dark" style="height: 5rem;">
|
116
|
+
|
117
|
+
<h6><%= content.group %>
|
118
|
+
|
119
|
+
<p class="small"><%= content.team %></p>
|
120
|
+
|
121
|
+
</h6>
|
122
|
+
|
123
|
+
</div>
|
124
|
+
|
125
|
+
<div class="card-body h6" style="height: 15rem;">
|
126
|
+
|
127
|
+
<p><%= link_to content.title.to_s.truncate(88), "#{content.url}" %></p>
|
128
|
+
|
129
|
+
<p class="card-text"><small class="text-muted"><%= content.author.to_s.truncate(26) %></small></p>
|
130
|
+
|
131
|
+
</div>
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
<div class="card-footer bg-transparent border-info d-flex align-items-center" style="height: 2rem;">
|
136
|
+
|
137
|
+
<!--いいね機能。_list_favorites.html.erbにrenderしています。-->
|
138
|
+
|
139
|
+
<div class="favorite-<%= content.id %> text-nowrap">
|
140
|
+
|
141
|
+
<%= render "favorites/list_favorite", content: content, favorites_count: content.favorites.length, cookies: cookies %>
|
142
|
+
|
143
|
+
</div>
|
144
|
+
|
145
|
+
</div>
|
146
|
+
|
147
|
+
</div>
|
148
|
+
|
149
|
+
</div>
|
150
|
+
|
151
|
+
<% end %>
|
152
|
+
|
153
|
+
</div>
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
<%= paginate @contents %>
|
158
|
+
|
159
|
+
</div>
|
160
|
+
|
161
|
+
</div>
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
**_list_favorites.html.erb**
|
166
|
+
|
167
|
+
```
|
102
168
|
|
103
169
|
<% if cookies.nil? %>
|
104
170
|
|
1
contentモデルとコントローラーを追記しています。
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,6 +40,36 @@
|
|
40
40
|
|
41
41
|
### 該当のソースコード
|
42
42
|
|
43
|
+
```ContentsModel
|
44
|
+
|
45
|
+
class Content < ApplicationRecord
|
46
|
+
|
47
|
+
has_many :favorites, dependent: :destroy
|
48
|
+
|
49
|
+
ransacker :favorites_count do
|
50
|
+
|
51
|
+
query = '(SELECT COUNT(favorites.content_id) FROM favorites where favorites.content_id = contents.id GROUP BY favorites.content_id)'
|
52
|
+
|
53
|
+
Arel.sql(query)
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
```FavoritesModel
|
62
|
+
|
63
|
+
class Favorite < ApplicationRecord
|
64
|
+
|
65
|
+
belongs_to :content
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
|
43
73
|
```Table
|
44
74
|
|
45
75
|
ActiveRecord::Schema.define(version: 2021_06_26_132254) do
|
@@ -102,7 +132,7 @@
|
|
102
132
|
|
103
133
|
```
|
104
134
|
|
105
|
-
```Controller
|
135
|
+
```FavoritesController
|
106
136
|
|
107
137
|
class FavoritesController < ApplicationController
|
108
138
|
|
@@ -134,6 +164,38 @@
|
|
134
164
|
|
135
165
|
|
136
166
|
|
167
|
+
```ContentsController
|
168
|
+
|
169
|
+
class ContentsController < ApplicationController
|
170
|
+
|
171
|
+
def index
|
172
|
+
|
173
|
+
if params[:q].present?
|
174
|
+
|
175
|
+
@q = Content.ransack(params[:q])
|
176
|
+
|
177
|
+
else
|
178
|
+
|
179
|
+
params[:q] = { sorts: 'favorites_count desc'}
|
180
|
+
|
181
|
+
@q = Content.ransack()
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
@contents = @q.result.page(params[:page])
|
188
|
+
|
189
|
+
@contents_all = Content.select(:author).distinct
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
```
|
196
|
+
|
197
|
+
|
198
|
+
|
137
199
|
|
138
200
|
|
139
201
|
### 試したこと
|