投稿一覧のページにて、カテゴリー別で表示する機能をつけたいです。
通常全ての投稿が一覧に表示されていますが、メニューの画像を押すと、
その画像が保有するcategory_idと同じcategory_idを持つ投稿のみの表示のような形式です。
index
1<%= link_to( image_tag("img.png"), controller: "posts", action: "index", category_id: 1) %> 2 3<% @posts.each do |post| %> 4 <div> 5 <%= post.user.name %> 6 <%= post.title %> 7 </div> 8<% end %>
上記のような場合、img.pngの画像をタップすると、
URLは「http://localhost:3000/posts/index?category_id=1」
となり、category_id:1が表示されています。
postModel
1class ProjectsController < ApplicationController 2 3def self.category(category_id) 4 if search 5 Post.where(category_id: category_id) 6 else 7 Post.all 8 end 9end 10end
postController
1def index 2 @posts = Post.where(params[:category_id]).order(created_at: :desc) 3end
上記のように、得たcategory_idの値と同じ投稿を表示してようとしまうが、
全ての投稿が一覧されたままで変化がありません。(エラーは出てきません)
恐れ入りますが、お力添えを頂ければ幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/21 03:25
2018/11/21 03:41
2018/11/21 04:37
2018/11/21 05:46 編集