###前提・実現したいこと
コメント(@comment)を投稿(@micropost)のアコーディオンメニューに表示したい。
そのために、このようなコードを成立させたい。
【page_controller.rb】
ruby
1class PageController < ApplicationController 2 3 def index 4 ・ 5 ・ 6 @micropost = Micropost.find(params[:id])★エラーでます。 7 @micropost = Micropost.includes(:user).find(params[:id])★エラーでます。 8 @comments = @micropost.comments.includes(:user).all 9 @comment = @micropost.comments.build(user_id: current_user.id) if current_user 10 ・ 11 ・ 12 end
###発生している問題・エラーメッセージ
ActiveRecord::RecordNotFound in PageController#index Couldn't find Micropost without an ID
Pageでは@micropostのidを受け取ることができず、エラーが出てしまいます。
Page#index内にこのような変数を置きたいです。
ruby
1@micropost = Micropost.find(params[:id]) 2@micropost = Micropost.includes(:user).find(params[:id])
###該当のソースコード
【page_controller.rb】
ruby
1class PageController < ApplicationController 2 3 def index 4 @micropost = current_user.microposts.build 5 @feed_items = current_user.feed.paginate(page: params[:page]) 6 @micropost = Micropost.find(params[:id])★エラーでます。 7 @micropost = Micropost.includes(:user).find(params[:id])★エラーでます。 8 @comments = @micropost.comments.includes(:user).all 9 @comment = @micropost.comments.build(user_id: current_user.id) if current_user 10 ・ 11 ・ 12 end
【index.html.erb】
ruby
1 2##アコーディオンメニュー 3<script> 4 $(function(){ 5 $("#acMenu dt").on("click", function() { 6 $(this).next().slideToggle(); 7 }); 8 }); 9</script> 10 11<!-- タイムライン --> 12<h3>Micropost Feed</h3> 13<%= render 'shared/feed' %> →_micropost.html.erbを呼び出します。 14
【_micropost.html.erb】
ruby
1<li id="micropost-<%= micropost.id %>"> 2 3 ・ 4 ・ 5 ・ 6 7 <dd> 8 <!-- コメント --> 9 <div> 10 <%= render 'comments/array' %> ←@commentを表示するパーシャルです。 11 <%= render 'calls/array' %> 12 <%= render 'says/array' %> 13 <%= render 'insists/array' %> 14 </div> 15 </dd> 16 </dl> 17 ・ 18 ・ 19 ・ 20 21 22</li>
【calls/_array.html.erb】
ruby
1<% @comments. each do |comment| %> 2 <div> 3 <strong><%= user_name(comment, @user) %></strong> 4 </br> 5 <p><%= body(comment) %></p> 6 <% if user_signed_in? && comment.user == current_user %> 7 <p><%= link_to 'Delete', comment_path(comment), method: :delete %></p> 8 <% end %> 9 </div> 10<% end %>
###補足情報(言語/FW/ツール等のバージョンなど)
・アコーディオンメニューは各投稿に設定してあり、
各投稿にコメントを投稿し、投稿を表示させたいです。
現状、コメントの投稿はできています。(データベース確認済み)
しかし、コメントの表示ができません。
おそらく、@micropostを正しくpage#indexに置いていないため、@micropostに紐づいた@commentを表示できない、ということだと思います(*´Д`)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。