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

質問編集履歴

5

画像修正

2020/06/29 14:17

投稿

fork_
fork_

スコア43

title CHANGED
File without changes
body CHANGED
@@ -6,8 +6,7 @@
6
6
  何故(recipe_id: recipe.id)と引数を取っているのでしょうか。
7
7
  selfはUserモデル自身を示している認識です。
8
8
 
9
-
10
- ![イメージ説明](fb43f2d751ae65ed879ab551e4855a7e.png)
9
+ ![イメージ説明](faf71bf28adc58eee9dcfef6aac97ac7.png)
11
10
  ```:ruby
12
11
  #Userモデル
13
12
  class User < ApplicationRecord

4

画像軽微修正

2020/06/29 14:17

投稿

fork_
fork_

スコア43

title CHANGED
File without changes
body CHANGED
@@ -7,8 +7,7 @@
7
7
  selfはUserモデル自身を示している認識です。
8
8
 
9
9
 
10
- ![イメージ説明](7ca58dffd8c0d86b2fe3609bae9e9d37.png)
10
+ ![イメージ説明](fb43f2d751ae65ed879ab551e4855a7e.png)
11
-
12
11
  ```:ruby
13
12
  #Userモデル
14
13
  class User < ApplicationRecord

3

画像修正しました

2020/06/29 14:14

投稿

fork_
fork_

スコア43

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,7 @@
7
7
  selfはUserモデル自身を示している認識です。
8
8
 
9
9
 
10
- ![イメージ説明](ec7a4f108462b4f3735e2d100b7376ab.png)
10
+ ![イメージ説明](7ca58dffd8c0d86b2fe3609bae9e9d37.png)
11
11
 
12
12
  ```:ruby
13
13
  #Userモデル

2

モデルを追加しました。

2020/06/29 14:11

投稿

fork_
fork_

スコア43

title CHANGED
File without changes
body CHANGED
@@ -10,6 +10,7 @@
10
10
  ![イメージ説明](ec7a4f108462b4f3735e2d100b7376ab.png)
11
11
 
12
12
  ```:ruby
13
+ #Userモデル
13
14
  class User < ApplicationRecord
14
15
 
15
16
  devise :database_authenticatable, :registerable,
@@ -26,8 +27,32 @@
26
27
 
27
28
  ```
28
29
 
30
+ ```
31
+ #Favoriteモデル
32
+ class Favorite < ApplicationRecord
33
+ belongs_to :user
34
+ belongs_to :recipe
29
35
 
36
+ validates_uniqueness_of :recipe_id, scope: :user_id
37
+ end
38
+
30
39
  ```
40
+
41
+ ```
42
+ #レシピモデル
43
+ class Recipe < ApplicationRecord
44
+ belongs_to :user
45
+ has_many :favorites, dependent: :destroy
46
+
47
+ with_options presence: true do
48
+ validates :title
49
+ validates :body
50
+ end
51
+ end
52
+
53
+ ```
54
+
55
+ ```
31
56
  <h1>recipe#index</h1>
32
57
  <p>Find me in app/views/recipe/index.html.erb</p>
33
58
 

1

補足点追加しました

2020/06/29 13:57

投稿

fork_
fork_

スコア43

title CHANGED
File without changes
body CHANGED
@@ -24,4 +24,30 @@
24
24
 
25
25
  end
26
26
 
27
- ```
27
+ ```
28
+
29
+
30
+ ```
31
+ <h1>recipe#index</h1>
32
+ <p>Find me in app/views/recipe/index.html.erb</p>
33
+
34
+ <% @recipes.each do |recipe| %>
35
+ <%= recipe.user.name %>
36
+ <%= recipe.title %>
37
+ <%= recipe.body %>
38
+
39
+
40
+ <% if Favorite.find_by(user_id: current_user.id, recipe_id: recipe.id) %>
41
+ <%= link_to "いいねを外す", recipe_favorites_path(recipe), method: :delete %>
42
+ <% else %>
43
+ <%= link_to "いいね", recipe_favorites_path(recipe), method: :post %>
44
+ <% end %>
45
+ <%= recipe.favorites.count %>
46
+ <% end %>
47
+ ```
48
+
49
+
50
+ ==============================================
51
+ 補足ですが、<% if current_user.already_favorited?(recipe) %>メソッドを使用せずに、view側で<% if Favorite.find_by(user_id: current_user.id, recipe_id: recipe.id) %>としても同じ挙動になりました。
52
+ この書き方の方が個人的にしっくりくるのですが、これは正しい書き方なのでしょうか。
53
+ Favoriteモデルの中に、ログインしているユーザーのIDと、レシピIDが入っていたら「いいね」をしているとみなす。そうでなかったら「いいね」を表示させる、という認識です。