adds comments to debates [#7]

This commit is contained in:
rgarcia
2015-07-18 17:38:57 +02:00
parent 05705f3174
commit 98463e4930
5 changed files with 60 additions and 1 deletions

View 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

View 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>

View 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 %>

View File

@@ -4,5 +4,14 @@
<p>Creado el: <%= l @debate.created_at.to_date %></p> <p>Creado el: <%= l @debate.created_at.to_date %></p>
</div> </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 'Edit', edit_debate_path(@debate) %> |
<%= link_to 'Back', debates_path %> <%= link_to 'Back', debates_path %>

View File

@@ -5,7 +5,9 @@ Rails.application.routes.draw do
# You can have the root of your site routed with "root" # You can have the root of your site routed with "root"
root 'welcome#index' root 'welcome#index'
resources :debates resources :debates do
resources :comments, only: :create
end
# Example of regular route: # Example of regular route:
# get 'products/:id' => 'catalog#view' # get 'products/:id' => 'catalog#view'