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

質問編集履歴

2

ご質問への回答のため編集しました。

2021/06/27 04:25

投稿

shutag
shutag

スコア1

title CHANGED
File without changes
body CHANGED
@@ -47,8 +47,41 @@
47
47
  end
48
48
  end
49
49
  ```
50
-
50
+ **index.html.erb**
51
- ```View
51
+ ```
52
+ <div class="album py-5 bg-light mt-3">
53
+ <div class="container">
54
+ <div class="row">
55
+ <% @contents.each do |content| %>
56
+ <div class="col-md-4">
57
+ <div class="card border-dark mb-4 shadow-sm">
58
+ <div class="card-header text-white bg-dark" style="height: 5rem;">
59
+ <h6><%= content.group %>
60
+ <p class="small"><%= content.team %></p>
61
+ </h6>
62
+ </div>
63
+ <div class="card-body h6" style="height: 15rem;">
64
+ <p><%= link_to content.title.to_s.truncate(88), "#{content.url}" %></p>
65
+ <p class="card-text"><small class="text-muted"><%= content.author.to_s.truncate(26) %></small></p>
66
+ </div>
67
+
68
+ <div class="card-footer bg-transparent border-info d-flex align-items-center" style="height: 2rem;">
69
+ <!--いいね機能。_list_favorites.html.erbにrenderしています。-->
70
+ <div class="favorite-<%= content.id %> text-nowrap">
71
+ <%= render "favorites/list_favorite", content: content, favorites_count: content.favorites.length, cookies: cookies %>
72
+ </div>
73
+ </div>
74
+ </div>
75
+ </div>
76
+ <% end %>
77
+ </div>
78
+
79
+ <%= paginate @contents %>
80
+ </div>
81
+ </div>
82
+ ```
83
+ **_list_favorites.html.erb**
84
+ ```
52
85
  <% if cookies.nil? %>
53
86
  <%= link_to content_favorites_path(content), method: :post, remote: true do %>
54
87
  <span class="far fa-heart like" id="like-<%= content.id %>"></span>

1

contentモデルとコントローラーを追記しています。

2021/06/27 04:24

投稿

shutag
shutag

スコア1

title CHANGED
File without changes
body CHANGED
@@ -19,6 +19,21 @@
19
19
  ![イメージ説明](8f6861a7cbc986f6692c0ce85e388ac9.jpeg)
20
20
 
21
21
  ### 該当のソースコード
22
+ ```ContentsModel
23
+ class Content < ApplicationRecord
24
+ has_many :favorites, dependent: :destroy
25
+ ransacker :favorites_count do
26
+ query = '(SELECT COUNT(favorites.content_id) FROM favorites where favorites.content_id = contents.id GROUP BY favorites.content_id)'
27
+ Arel.sql(query)
28
+ end
29
+ end
30
+ ```
31
+ ```FavoritesModel
32
+ class Favorite < ApplicationRecord
33
+ belongs_to :content
34
+ end
35
+ ```
36
+
22
37
  ```Table
23
38
  ActiveRecord::Schema.define(version: 2021_06_26_132254) do
24
39
  create_table "contents", force: :cascade do |t|
@@ -50,7 +65,7 @@
50
65
  <% end %>
51
66
  <%= favorites_count %>
52
67
  ```
53
- ```Controller
68
+ ```FavoritesController
54
69
  class FavoritesController < ApplicationController
55
70
  def create
56
71
  @content = Content.find(params[:content_id])
@@ -66,7 +81,23 @@
66
81
  end
67
82
  ```
68
83
 
84
+ ```ContentsController
85
+ class ContentsController < ApplicationController
86
+ def index
87
+ if params[:q].present?
88
+ @q = Content.ransack(params[:q])
89
+ else
90
+ params[:q] = { sorts: 'favorites_count desc'}
91
+ @q = Content.ransack()
92
+ end
93
+
94
+ @contents = @q.result.page(params[:page])
95
+ @contents_all = Content.select(:author).distinct
96
+ end
97
+ end
98
+ ```
69
99
 
100
+
70
101
  ### 試したこと
71
102
  以下のqiitaの投稿も参考にしながら作成し、コードのずれもないはずなのですが、原因がわかりません。。。
72
103