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

回答編集履歴

1

詳細追記

2018/11/21 03:39

投稿

hellomartha
hellomartha

スコア329

answer CHANGED
@@ -1,9 +1,31 @@
1
+ >ここ記載ミスってます?
2
+ >Post.category(params[:category_id])ですかね
3
+ >searchがnilで全て表示されている可能性がありそうですが。
4
+
5
+ 回答の仕方が悪かったです、すみません。
6
+ 修正方法を2種類記載します。
7
+
8
+ 修正1
9
+
1
10
  ```ruby
2
11
  def index
3
- @posts = Post.where(params[:category_id]).order(created_at: :desc)
12
+ @posts = Post.where(id: params[:category_id]).order(created_at: :desc)
4
13
  end
5
14
  ```
6
15
 
16
+ 修正2
17
+ ```ruby
7
- ここ記載ミスってます?
18
+ def index
19
+ @posts = Post.category(params[:category_id]).order(created_at: :desc)
20
+ end
21
+ ```
22
+
23
+ ```ruby
24
+ def self.category(category_id)
25
+  if search #⇐ここがnil/falseになってないか確認する
8
- Post.category(params[:category_id])ですかね
26
+   Post.where(category_id: category_id)
27
+  else
9
- searchがnilで全て表示されている可能性がありそうですが。
28
+   Post.all
29
+  end
30
+ end
31
+ ```