やりたいこと
トップページにコメント一覧を表示させる
問題点
https://gyazo.com/e07751929a071e6d52390b314ae87ae0の画面で、コメント投稿をしたところ、コメントがトップページに表示されません(コンソール上ではコメントが保存されいています)。
現在のコードは以下の通りです。
toppages.controller.rb
class ToppagesController < ApplicationController def index if logged_in? @user = current_user @post = current_user.posts.build # form_for 用 @posts = current_user.feed_posts.order('created_at DESC').page(params[:page]) @post_comment = current_user.post_comments.build @post_comments = @post.post_comments.order('created_at DESC').page(params[:page]) end end end
posts_controller.rb
class PostsController < ApplicationController before_action :require_user_logged_in before_action :correct_user, only: [:destroy] def index @post_comment = current_user.post_comments.build end def show @post = Post.includes(:user).find(params[:id]) end def create @post = current_user.posts.build(post_params) if @post.save flash[:success] = 'メッセージを投稿しました。' redirect_to root_url else @posts = current_user.feed_posts.order('created_at DESC').page(params[:page]) flash.now[:danger] = 'メッセージの投稿に失敗しました。' render 'toppages/index' end end def destroy @post.destroy flash[:success] = 'メッセージを削除しました。' redirect_back(fallback_location: root_path) end private def post_params params.require(:post).permit(:picture, :content) end def correct_user @post = current_user.posts.find_by(id: params[:id]) unless @post redirect_to root_url end end end
post_comments.controller.rb
class PostCommentsController < ApplicationController before_action :require_user_logged_in before_action :correct_user, only: [:destroy] def create @post = Post.find(params[:post_id]) @post_comment = @post.post_comments.create(post_comment_params) @post_comment.user_id = current_user.id #@post_comment = current_user.post_comments.build(post_comment_params) #@comment = Comment.create(text: comment_params[:text], post_id: comment_params[:post_id], user_id: current_user.id) if @post_comment.save flash[:success] = "コメントしました。" #redirect_to "/posts/#{@comment.post.id}" #redirect_to post_comments_path(@post.id) #redirect_to :action =>"new" redirect_to root_url else @post_comments = @post.post_comments.order('created_at DESC').page(params[:page]) flash.now[:danger] = 'コメントの投稿に失敗しました。' render 'toppages/index' end end def destroy @post_comment.destroy flash[:success] = 'コメントを削除しました。' redirect_back(fallback_location: root_path) end private # Use callbacks to share common setup or constraints between actions. def set_post_comment @post = Post.find(params[:post_id]) @post_comment = @post.post_comments.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def post_comment_params params.require(:post_comment).permit(:user_id, :post_id, :content) end def correct_user @post_comment = current_user.post_comments.find_by(id: params[:id]) unless @post_comment redirect_to root_path end end end
comments/_comments.html.erb
<ul class="media-list"> <% @post_comments.each do |post_comment| %> <% user = post_comment.user %> <div> <p>投稿者:<%= link_to user.name, user_path(user) %> <span class="text-muted">commented at <%= post_comment.created_at %></span></p> </div> <p><%= post_comment.content %></p> <div> <% if current_user == post_comment.user %> <%= link_to "削除", post_comment, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %> <% end %> </div> <% end %> <%= paginate @post_comments %> </ul>
posts/_posts.html.erb
<ul class="media-list"> <% posts.each do |post| %> <% user = post.user %> <li class="media"> <div class="media-left"> <img class="media-object img-rounded" src="<%= gravatar_url(user, options = { size: 50 }) %>" alt=""> </div> <div class="media-body"> <div> <%= link_to user.name, user_path(user) %> <span class="text-muted">posted at <%= post.created_at %></span> </div> <div> <p><%= image_tag post.picture,:size =>"280x210" %></p> <p><%= post.content %></p> <%= render 'comments/comments', post_comments: @post_comments %> <br/> <% if current_user %> <%= form_for [post, Comment.new] do |form| %> <%= form.text_area :content, cols: "30", placeholder: "コメントする", rows: "2" %> <%= form.submit "コメントの投稿" %> <% end %> <% end %> </div> <div> <% if current_user == post.user %> <%= link_to "削除", post, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %> <% end %> </div> </div> </li> <% end %> <%= paginate posts %> </ul>
toppages/index.html.erb
<% if logged_in? %> <div class="row"> <aside class="col-md-4"> <%= form_for(@post, html: {multipart: true}) do |f| %> <div class="form-group"> <%= f.label :picture, '写真' %> <%= f.file_field :picture %><br /> <%= f.label :content, 'コンテンツ' %> <%= f.text_area :content, class: 'form-control', rows: 5 %> </div> <%= f.submit '投稿', class: 'btn btn-primary btn-md' %> <% end %> </aside> <div class="col-xs-8"> <%= render 'posts/posts', posts: @posts %> </div> </div> <% else %> <div class="cover"> <div class="cover-inner"> <div class="cover-contents"> <h1>毎日のつながりは、ここから始まる</h1>> <%= link_to 'まずは会員登録から', signup_path, class: 'btn btn-success btn-md' %> <%= link_to '会員の方はこちら', login_path, class: 'btn btn-success btn-md' %> </div> </div> </div> <% end %>
toppages_controller.rbに
@post_comment = current_user.post_comments.build @post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
を入れれば表示されると思っていましたが、全く表示されませんでした。
他にも色々試しましたが、それでもコメントは表示されませんでした。
これ以上、トップページに投稿したコメントを表示させる方法がわからないので、どなたかご教示をお願いできませんか?
回答1件
あなたの回答
tips
プレビュー