Reputation: 117
I am building a basic website with forum functionality. I am trying to implement a feature in the PostController#Show that will display each comment associated with that post. However, I keep getting an error along the lines of Can't Find ______ Without An ID. Here is what I have:
class PostsController < ApplicationController
def show
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
@comments = Comment.find(params[:post_id])
end
I have tried multiple variations for @comments, but similar errors occur.
<h1><%= markdown_to_html @post.title %></h1>
<div class="row">
<div class="col-md-8">
<small>
<%= image_tag(@post.user.avatar.tiny.url) if @post.user.avatar? %>
submitted <%= time_ago_in_words(@post.created_at) %> age by
<%= @post.user.name %>
</small>
<p><%= markdown_to_html( @post.body) %></p>
<p><%= markdown_to_html( @comments ) %></p>
</div
<div class="col-md-4">
<% if policy(@post).edit? %>
<%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
<% end %>
</div>
</div>
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create]
end
end
get 'about' => 'welcome#about'
get 'contact' => 'welcome#contact'
root to: 'welcome#index'
end
I assume this has something to do with nesting Comments within Posts within Topics and not using the correct syntax. Can anyone point me in the right direction?
Thank you,
Matt
Upvotes: 0
Views: 269
Reputation: 10273
Couldn't you just do, in your view:
@post.comments.each do |comment|
Alternatively in your controller:
@comments = @post.comments
I'm assuming you have an association set up.
Upvotes: 1