前提
ruby on railsで過去の出来事について投稿するSNSのようなものを製作しています。
ユーザー一覧ページから、選択したユーザーの投稿一覧ページへ飛ぶようにしているのですが選択したユーザー以外の投稿も一緒に表示されてしまいます。
実現したいこと
- ユーザー一覧ページから特定のユーザーを選択すると、その人の投稿した記事のみが一覧で表示されるようにしたいです。
発生している問題・エラーメッセージ
エラーメッセージは出ていません。
ユーザーネームの横の「○○の詳細へ」をクリックした時、コマンドプロンプトは以下のように動いています。(user_id === 3の詳細ページへ飛ぼうとした場合)
Started GET "/posts/index_public.3" for ::1 at 2022-04-16 23:24:04 +0900 Processing by PostsController#index_public as */* User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] Rendering layout layouts/application.html.erb Rendering posts/index_public.html.erb within layouts/application Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."setting" = ? ORDER BY "posts"."date" ASC [["setting", 1]] ↳ app/views/posts/index_public.html.erb:2 Rendered posts/index_public.html.erb within layouts/application (Duration: 12.2ms | Allocations: 843) [Webpacker] Everything's up-to-date. Nothing to do Rendered layout layouts/application.html.erb (Duration: 82.3ms | Allocations: 5570) Completed 200 OK in 100ms (Views: 91.4ms | ActiveRecord: 0.6ms | Allocations: 7168)
該当のソースコード
1,ユーザー一覧のviewページ
views/others/index.html.erb
1<h3>他のユーザー一覧</h3> 2<p> 3自分は<%= @current_user.name %>です 4<% @users.each do |t| %> 5<%= t.name %> 6<%= t.id %> 7<%= link_to "#{t.name}の詳細へ", index_public_posts_path(t.id) %> 8<% end %> 9</p>
2,選択したユーザーの投稿一覧viewページ
views/posts/index_public.html.erb
1<div class="posts-container"> 2 <% @posts.each do |t| %> 3 <div class="post"> 4 <%= t.title %> 5 <%= link_to "詳細へ", show_public_post_path(t.id) %> 6 </div> 7 <% end %> 8</div>
3,コントローラー
others_controller.rb
1def index 2 @users = User.where.not(id: current_user.id) 3 @current_user = User.find_by(id: current_user.id) 4end
posts_controller.rb
1 def index_public 2 @posts = Post.where(params[:id]).where(setting: "true").order(date: "ASC") 3 end
4,ルーティング
routes.rb
1 resources :posts do 2 resources :likes, only: [:create, :destroy] 3 collection do 4 get :index_private, controller: 'posts', action: 'index_private' 5 get :index_public, controller: 'posts', action: 'index_public' 6 end 7 member do 8 get :show_private, controller: 'posts', action: 'show_private' 9 get :show_public, controller: 'posts', action: 'show_public' 10 end 11 end 12 13 get 'others/index' => 'others#index'
試したこと
user_idがうまく渡せていないのではないかと考え、ルーティングの「get :index_public, controller: 'posts', action: 'index_public'」の部分をmember doの方に記載場所を変更したのですがうまくいきませんでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/17 01:36
2022/04/17 01:43
2022/04/17 02:35