前提・実現したいこと
掲示板のトピックを表示し、その下に新規投稿欄とコメントを表示したいです。とりあえcommentsというテーブルを作成し、routingやコントローラーを設定、partial (主はpartialが何なのかもはっきりと理解してはいません)で繋いでみようかと思ったんですが、なかなかうまくいきません。なにか有効な方法を教えていただけると幸いです。。
発生している問題・エラーメッセージ
以下のエラーは、comments/_new.html.erbは使わず、_index.html.erbのみをpartialで繋いだ場合です。
comments/_index.html.erb
undefined method `each' for nil:NilClass
Extracted source (around line #4):
<div class = "container"> <p class="users-heading">コメント一覧</p> <% @comments.each do |comment| %> <div class = "comments-index-item"> <%= link_to(comment.com_content, "/comments/#{comment.id}") %> </div>該当のソースコード
comments/_index.html.erb
<div class = "main comments-index"> <div class = "container"> <p class="users-heading">コメント一覧</p> <% @comments.each do |comment| %> <div class = "comments-index-item"> <%= link_to(comment.com_content, "/comments/#{comment.id}") %> </div> <% end %> </div> </div>/posts/show.html.erb
<header> <div class="main posts-show"> <div class="container"> <div class="posts-show-item"> <p> <%= @post.content %> </p> <div class="post-time"> <%= @post.created_at %> </div> <div class="post-menus"> <%= link_to("削除", "/posts/#{@post.id}/destroy")%> </div> <div> "/comments/new"に繋げる。 </div> </div> </div> </div> </header> <body> <% render("/comments/index") %> </body>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/06 15:03
2019/02/18 16:00