質問編集履歴
6
change
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,124 +1,6 @@
|
|
1
1
|

|
2
2
|
|
3
|
-
###likes.html.erb
|
4
|
-
```ここに言語を入力
|
5
|
-
<%= render "partial/navbar" %>
|
6
3
|
|
7
|
-
<div class="user-show">
|
8
|
-
<div class="container">
|
9
|
-
<div class="row">
|
10
|
-
<div class="col-lg-3 col-md-4">
|
11
|
-
<div class="user-profile row-space-4 text-center">
|
12
|
-
<% if current_user.image %>
|
13
|
-
<img src="<%= "/user_images/#{@user.image}" %>" class="profile-full img-circle">
|
14
|
-
<% else %>
|
15
|
-
<p><%= link_to "プロフィール画像を追加する", edit_user_path(current_user), class:"profile-full img-circle" %></p>
|
16
|
-
<% end %>
|
17
|
-
</div>
|
18
|
-
</div>
|
19
|
-
|
20
|
-
<div class="col-lg-9 col-md-8 col-sm-12">
|
21
|
-
<div class="user-title">
|
22
|
-
<% if current_user.name %>
|
23
|
-
<h1><%= @user.name%></h1>
|
24
|
-
<% else %>
|
25
|
-
<p><%= link_to "ユーザー名を追加して下さい", edit_user_path(current_user) %></p>
|
26
|
-
<% end %>
|
27
|
-
<% if current_user.id == @user.id %>
|
28
|
-
<%= link_to "プロフィール編集", edit_user_path(current_user), class: "btn btn-default btn-full" %>
|
29
|
-
<%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: "btn btn-default btn-full" %>
|
30
|
-
<% end %>
|
31
|
-
</div>
|
32
|
-
|
33
|
-
<div class="user-description">
|
34
|
-
自己紹介:
|
35
|
-
<% if current_user.description %>
|
36
|
-
<%= @user.description %>
|
37
|
-
<% else %>
|
38
|
-
自己紹介文が空白です。<%= link_to "自己紹介を書く", edit_user_path(current_user) %>
|
39
|
-
<% end %>
|
40
|
-
</div>
|
41
|
-
</div>
|
42
|
-
</div>
|
43
|
-
</div>
|
44
|
-
|
45
|
-
|
46
|
-
<ul class="user-tabs">
|
47
|
-
<li><%= link_to("質問", "/users/#{@user.id}") %></li>
|
48
|
-
<li class="active"><%= link_to("お気に入り", "/users/#{@user.id}/likes") %></li>
|
49
|
-
</ul>
|
50
|
-
|
51
|
-
<br>
|
52
|
-
<hr>
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
<% @likes.each do |like| %>
|
57
|
-
<%
|
58
|
-
question = User.find_by(id: like.question_id)
|
59
|
-
continue if question.nil?
|
60
|
-
%>
|
61
|
-
|
62
|
-
<div class="card">
|
63
|
-
<div class="card-block">
|
64
|
-
<h4 class="card-title">
|
65
|
-
<img src="<%= "/user_images/#{question.user.image}" %>" class="img-circle profile-1">
|
66
|
-
</h4>
|
67
|
-
<h6 class="card-subtitle mb-2 text-muted">
|
68
|
-
<%= link_to(question.user.name, "/users/#{question.user.id}") %>
|
69
|
-
</h6>
|
70
|
-
<hr>
|
71
|
-
<p class="card-text">
|
72
|
-
<%= link_to(question.content, "/questions/#{question.id}") %>
|
73
|
-
</p>
|
74
|
-
<p class="card-link">
|
75
|
-
<%= question.created_at.strftime('%Y/%m/%d %H:%M') %>
|
76
|
-
</p>
|
77
|
-
</div>
|
78
|
-
</div>
|
79
|
-
<% end %>
|
80
|
-
<% end %>
|
81
|
-
</div>
|
82
|
-
</div>
|
83
|
-
|
84
|
-
```
|
85
|
-
|
86
|
-
###user_controller.rb
|
87
|
-
```ここに言語を入力
|
88
|
-
class UsersController < ApplicationController
|
89
|
-
def likes
|
90
|
-
@user = User.find_by(id: params[:id])
|
91
|
-
@likes = Like.where(user_id: @user.id)
|
92
|
-
end
|
93
|
-
```
|
94
|
-
|
95
|
-
どの部分がエラーになっているのでしょうか?
|
96
|
-
また、どう修正したら良いのでしょうか?
|
97
|
-
教えてください!
|
98
|
-
|
99
|
-
###model
|
100
|
-
**user.rb**
|
101
|
-
```ここに言語を入力
|
102
|
-
class User < ApplicationRecord
|
103
|
-
has_many :questions
|
104
|
-
has_many :answers
|
105
|
-
has_many :likes
|
106
|
-
end
|
107
|
-
```
|
108
|
-
**like.rb**
|
109
|
-
```ここに言語を入力
|
110
|
-
class Like < ApplicationRecord
|
111
|
-
belongs_to :question
|
112
|
-
end
|
113
|
-
```
|
114
|
-
**question.rb**
|
115
|
-
```ここに言語を入力
|
116
|
-
class Question < ApplicationRecord
|
117
|
-
belongs_to :user
|
118
|
-
has_many :answers
|
119
|
-
end
|
120
|
-
```
|
121
|
-
|
122
4
|
###やりたいこと
|
123
5
|
|
124
6
|
ユーザーがlikeした、questionを全て取り出し、表示したいのですが、うまく行きません。
|
5
like add
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,9 +2,63 @@
|
|
2
2
|
|
3
3
|
###likes.html.erb
|
4
4
|
```ここに言語を入力
|
5
|
-
<% @likes.each do |like| %>
|
6
|
-
|
5
|
+
<%= render "partial/navbar" %>
|
7
6
|
|
7
|
+
<div class="user-show">
|
8
|
+
<div class="container">
|
9
|
+
<div class="row">
|
10
|
+
<div class="col-lg-3 col-md-4">
|
11
|
+
<div class="user-profile row-space-4 text-center">
|
12
|
+
<% if current_user.image %>
|
13
|
+
<img src="<%= "/user_images/#{@user.image}" %>" class="profile-full img-circle">
|
14
|
+
<% else %>
|
15
|
+
<p><%= link_to "プロフィール画像を追加する", edit_user_path(current_user), class:"profile-full img-circle" %></p>
|
16
|
+
<% end %>
|
17
|
+
</div>
|
18
|
+
</div>
|
19
|
+
|
20
|
+
<div class="col-lg-9 col-md-8 col-sm-12">
|
21
|
+
<div class="user-title">
|
22
|
+
<% if current_user.name %>
|
23
|
+
<h1><%= @user.name%></h1>
|
24
|
+
<% else %>
|
25
|
+
<p><%= link_to "ユーザー名を追加して下さい", edit_user_path(current_user) %></p>
|
26
|
+
<% end %>
|
27
|
+
<% if current_user.id == @user.id %>
|
28
|
+
<%= link_to "プロフィール編集", edit_user_path(current_user), class: "btn btn-default btn-full" %>
|
29
|
+
<%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: "btn btn-default btn-full" %>
|
30
|
+
<% end %>
|
31
|
+
</div>
|
32
|
+
|
33
|
+
<div class="user-description">
|
34
|
+
自己紹介:
|
35
|
+
<% if current_user.description %>
|
36
|
+
<%= @user.description %>
|
37
|
+
<% else %>
|
38
|
+
自己紹介文が空白です。<%= link_to "自己紹介を書く", edit_user_path(current_user) %>
|
39
|
+
<% end %>
|
40
|
+
</div>
|
41
|
+
</div>
|
42
|
+
</div>
|
43
|
+
</div>
|
44
|
+
|
45
|
+
|
46
|
+
<ul class="user-tabs">
|
47
|
+
<li><%= link_to("質問", "/users/#{@user.id}") %></li>
|
48
|
+
<li class="active"><%= link_to("お気に入り", "/users/#{@user.id}/likes") %></li>
|
49
|
+
</ul>
|
50
|
+
|
51
|
+
<br>
|
52
|
+
<hr>
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
<% @likes.each do |like| %>
|
57
|
+
<%
|
58
|
+
question = User.find_by(id: like.question_id)
|
59
|
+
continue if question.nil?
|
60
|
+
%>
|
61
|
+
|
8
62
|
<div class="card">
|
9
63
|
<div class="card-block">
|
10
64
|
<h4 class="card-title">
|
@@ -22,7 +76,11 @@
|
|
22
76
|
</p>
|
23
77
|
</div>
|
24
78
|
</div>
|
25
|
-
<% end %>
|
79
|
+
<% end %>
|
80
|
+
<% end %>
|
81
|
+
</div>
|
82
|
+
</div>
|
83
|
+
|
26
84
|
```
|
27
85
|
|
28
86
|
###user_controller.rb
|
4
add
title
CHANGED
File without changes
|
body
CHANGED
@@ -59,4 +59,8 @@
|
|
59
59
|
belongs_to :user
|
60
60
|
has_many :answers
|
61
61
|
end
|
62
|
-
```
|
62
|
+
```
|
63
|
+
|
64
|
+
###やりたいこと
|
65
|
+
|
66
|
+
ユーザーがlikeした、questionを全て取り出し、表示したいのですが、うまく行きません。
|
3
change model
title
CHANGED
File without changes
|
body
CHANGED
@@ -49,7 +49,7 @@
|
|
49
49
|
```
|
50
50
|
**like.rb**
|
51
51
|
```ここに言語を入力
|
52
|
-
class
|
52
|
+
class Like < ApplicationRecord
|
53
53
|
belongs_to :question
|
54
54
|
end
|
55
55
|
```
|
2
add model class
title
CHANGED
File without changes
|
body
CHANGED
@@ -41,16 +41,22 @@
|
|
41
41
|
###model
|
42
42
|
**user.rb**
|
43
43
|
```ここに言語を入力
|
44
|
+
class User < ApplicationRecord
|
44
45
|
has_many :questions
|
45
46
|
has_many :answers
|
46
47
|
has_many :likes
|
48
|
+
end
|
47
49
|
```
|
48
50
|
**like.rb**
|
49
51
|
```ここに言語を入力
|
52
|
+
class like < ApplicationRecord
|
50
53
|
belongs_to :question
|
54
|
+
end
|
51
55
|
```
|
52
56
|
**question.rb**
|
53
57
|
```ここに言語を入力
|
58
|
+
class Question < ApplicationRecord
|
54
59
|
belongs_to :user
|
55
60
|
has_many :answers
|
61
|
+
end
|
56
62
|
```
|
1
modelの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -36,4 +36,21 @@
|
|
36
36
|
|
37
37
|
どの部分がエラーになっているのでしょうか?
|
38
38
|
また、どう修正したら良いのでしょうか?
|
39
|
-
教えてください!
|
39
|
+
教えてください!
|
40
|
+
|
41
|
+
###model
|
42
|
+
**user.rb**
|
43
|
+
```ここに言語を入力
|
44
|
+
has_many :questions
|
45
|
+
has_many :answers
|
46
|
+
has_many :likes
|
47
|
+
```
|
48
|
+
**like.rb**
|
49
|
+
```ここに言語を入力
|
50
|
+
belongs_to :question
|
51
|
+
```
|
52
|
+
**question.rb**
|
53
|
+
```ここに言語を入力
|
54
|
+
belongs_to :user
|
55
|
+
has_many :answers
|
56
|
+
```
|