Commit a85840fd authored by John Noel's avatar John Noel

Finished comments

parent ee22f4fc
class CommentsController < ApplicationController class CommentsController < ApplicationController
def new def new
@post = Post.find_by(id: params[:post_id])
@comment = @post.comments.new
end 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> <h3>New Comment</h3>
<p>Find me in app/views/comments/new.html.erb</p>
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 @@ ...@@ -16,7 +16,8 @@
<th>Title</th> <th>Title</th>
<th>Content</th> <th>Content</th>
<th>Posted by</th> <th>Posted by</th>
<th colspan="3"></th> <th># of Comments</th>
<th colspan="4"></th>
</thead> </thead>
<tbody> <tbody>
<% @posts.each do |post| %> <% @posts.each do |post| %>
...@@ -30,6 +31,9 @@ ...@@ -30,6 +31,9 @@
<td> <td>
<%= post.user %> <%= post.user %>
</td> </td>
<td>
<%= post.comments.count %>
</td>
<td> <td>
<%= link_to "Show", post_path(post), class: "btn btn-primary" %> <%= link_to "Show", post_path(post), class: "btn btn-primary" %>
</td> </td>
...@@ -39,6 +43,9 @@ ...@@ -39,6 +43,9 @@
<td> <td>
<%= link_to "Edit", edit_post_path(post), class: "btn btn-primary" %> <%= link_to "Edit", edit_post_path(post), class: "btn btn-primary" %>
</td> </td>
<td>
<%= link_to "Add Comment", new_post_comment_path(post), class: "btn btn-info" %>
</td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
......
...@@ -7,4 +7,22 @@ ...@@ -7,4 +7,22 @@
<%= @post.content %> <%= @post.content %>
</p> </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" %> <%= 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