Commit a85840fd authored by John Noel's avatar John Noel

Finished comments

parent ee22f4fc
class CommentsController < ApplicationController
def new
@post = Post.find_by(id: params[:post_id])
@comment = @post.comments.new
end
end
def create
@post = Post.find_by(id: params[:post_id])
@comment = @post.comments.new(comment_params)
@comment.user = current_user
if @comment.save
redirect_to post_path(@post), notice: "Comment has been added."
else
render 'new'
end
end
private
def comment_params
params.require(:comment).permit!
end
end
\ No newline at end of file
<h1>Comments#new</h1>
<p>Find me in app/views/comments/new.html.erb</p>
<h3>New Comment</h3>
Comment for: <%= @post.title %>
<%= simple_form_for([@post, @comment]) do |f| %>
<%= f.input :comment %>
<%= f.button :submit, "Add Comment" %>
<% end %>
\ No newline at end of file
......@@ -16,7 +16,8 @@
<th>Title</th>
<th>Content</th>
<th>Posted by</th>
<th colspan="3"></th>
<th># of Comments</th>
<th colspan="4"></th>
</thead>
<tbody>
<% @posts.each do |post| %>
......@@ -30,6 +31,9 @@
<td>
<%= post.user %>
</td>
<td>
<%= post.comments.count %>
</td>
<td>
<%= link_to "Show", post_path(post), class: "btn btn-primary" %>
</td>
......@@ -39,6 +43,9 @@
<td>
<%= link_to "Edit", edit_post_path(post), class: "btn btn-primary" %>
</td>
<td>
<%= link_to "Add Comment", new_post_comment_path(post), class: "btn btn-info" %>
</td>
</tr>
<% end %>
</tbody>
......
......@@ -7,4 +7,22 @@
<%= @post.content %>
</p>
<ul>
<% @post.comments.each do |comment| %>
<li>
<%= comment.comment %>
<br>
<small>
<%= comment.user %><br>
<%= time_ago_in_words(comment.created_at) %> ago
</small>
</li>
<% end %>
</ul>
<%= simple_form_for([@post, @post.comments.build]) do |f| %>
<%= f.input :comment %>
<%= f.button :submit, "Add Comment" %>
<% end %>
<%= link_to "Back", posts_path, class: "btn btn-default" %>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment