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

回答編集履歴

1

追記

2019/09/13 08:56

投稿

rhiroe
rhiroe

スコア2352

answer CHANGED
@@ -58,4 +58,42 @@
58
58
  3回目: id == 1 の note
59
59
  ```
60
60
  となり`id == 1`の`note`が複数回できてきます。
61
- これを`each`が複数回実行されたと勘違いされているのでは?
61
+ これを`each`が複数回実行されたと勘違いされているのでは?
62
+
63
+ ### 追記
64
+ 一応確認しておきますが、あなたの書いたHTMLは以下のようなものです。
65
+ 見易いよう必要なところだけ抜粋して、インデントがぐちゃぐちゃだったのでslimに直して整形してあります。
66
+ ```slim
67
+ - @note.each do |note|
68
+ .mylist
69
+ // ----------------------------------------------------------
70
+ h2 = link_to note.title, note, class: 'mylist-buttom'
71
+ - if @user.id == current_user.id
72
+ .mylist-right
73
+ - if @user.id == current_user.id
74
+ p = link_to '編集 ', edit_note_path(note),class: 'mylist-buttom'
75
+ p = link_to '削除', note, method: :delete, data: { confirm: 'Are you sure?' } ,class: 'mylist-buttom'
76
+ // ----------------------------------------------------------
77
+
78
+
79
+ - @goods.each do |good|
80
+ .mylist
81
+ - note = Note.find_by(id: good.note_id)
82
+ // ----------------------------------------------------------
83
+ h2 = link_to(note.title, "/notes/#{note.id}")
84
+ .mylist-right
85
+ p = link_to 'Show', note, class: 'buttom'
86
+ // ----------------------------------------------------------
87
+
88
+
89
+ - @comments.uniq.each do |c|
90
+ .mylist
91
+ - note = Note.find_by(id: c.note_id)
92
+ // ----------------------------------------------------------
93
+ h2 = link_to(note.title, "/notes/#{note.id}")
94
+ .mylist-right
95
+ p = link_to 'Show', note, class: 'u-buttom'
96
+ // ----------------------------------------------------------
97
+ ```
98
+ 見ての通り、似たようなものを3回に渡って行なっています。特に2つ目と3つ目は`@comments`と`@goods`の中身次第では全く同じになる可能性もあります。
99
+ まさかとは思いますが、そういう話ではないですよね?