adds comments to debates [#7]
This commit is contained in:
28
app/controllers/comments_controller.rb
Normal file
28
app/controllers/comments_controller.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
class CommentsController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
before_action :set_debate, :set_parent
|
||||
|
||||
def create
|
||||
comment = Comment.build(@debate, current_user, params[:comment][:body])
|
||||
comment.save!
|
||||
comment.move_to_child_of(@parent) if reply?
|
||||
redirect_to @debate, notice: "Comentario guardado"
|
||||
end
|
||||
|
||||
private
|
||||
def comment_params
|
||||
params.require(:comments).permit(:commentable_type, :commentable_id, :body)
|
||||
end
|
||||
|
||||
def set_debate
|
||||
@debate = Debate.find(params[:debate_id])
|
||||
end
|
||||
|
||||
def set_parent
|
||||
@parent = Comment.find_parent(params[:comment])
|
||||
end
|
||||
|
||||
def reply?
|
||||
@parent.class == Comment
|
||||
end
|
||||
end
|
||||
13
app/views/comments/_comment.html.erb
Normal file
13
app/views/comments/_comment.html.erb
Normal file
@@ -0,0 +1,13 @@
|
||||
<div id="comment-<%= comment.id %>" class='comment' style="padding-bottom:30px">
|
||||
<p><%= comment.user.name %></p>
|
||||
<p>hace <%= time_ago_in_words(comment.created_at) %></p>
|
||||
<p><%= comment.body %></p>
|
||||
|
||||
<div style="margin-left:50px">
|
||||
<%= render comment.children %>
|
||||
</div>
|
||||
|
||||
<div style="margin-left:50px">
|
||||
<%= render 'comments/form', parent: comment %>
|
||||
</div>
|
||||
</div>
|
||||
7
app/views/comments/_form.html.erb
Normal file
7
app/views/comments/_form.html.erb
Normal file
@@ -0,0 +1,7 @@
|
||||
<%= form_for [@debate, Comment.new] do |f| %>
|
||||
<%= f.text_area :body %>
|
||||
<%= f.hidden_field :commentable_type, value: parent.class %>
|
||||
<%= f.hidden_field :commentable_id, value: parent.id %>
|
||||
|
||||
<%= f.submit 'Publicar comentario' %>
|
||||
<% end %>
|
||||
@@ -4,5 +4,14 @@
|
||||
<p>Creado el: <%= l @debate.created_at.to_date %></p>
|
||||
</div>
|
||||
|
||||
Deja tu comentario
|
||||
<%= render 'comments/form', parent: @debate %>
|
||||
|
||||
<div id="comments" style="padding-top:20px">
|
||||
<h2>Comentarios</h2>
|
||||
<%= render @debate.root_comments %>
|
||||
</div>
|
||||
|
||||
<br/><br/>
|
||||
<%= link_to 'Edit', edit_debate_path(@debate) %> |
|
||||
<%= link_to 'Back', debates_path %>
|
||||
@@ -5,7 +5,9 @@ Rails.application.routes.draw do
|
||||
|
||||
# You can have the root of your site routed with "root"
|
||||
root 'welcome#index'
|
||||
resources :debates
|
||||
resources :debates do
|
||||
resources :comments, only: :create
|
||||
end
|
||||
|
||||
# Example of regular route:
|
||||
# get 'products/:id' => 'catalog#view'
|
||||
|
||||
Reference in New Issue
Block a user