diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 000000000..91a92e5ba --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -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 \ No newline at end of file diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb new file mode 100644 index 000000000..a27a689c9 --- /dev/null +++ b/app/views/comments/_comment.html.erb @@ -0,0 +1,13 @@ +
+

<%= comment.user.name %>

+

hace <%= time_ago_in_words(comment.created_at) %>

+

<%= comment.body %>

+ +
+ <%= render comment.children %> +
+ +
+ <%= render 'comments/form', parent: comment %> +
+
\ No newline at end of file diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb new file mode 100644 index 000000000..4ef99b4b0 --- /dev/null +++ b/app/views/comments/_form.html.erb @@ -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 %> \ No newline at end of file diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 777b22d1b..770be127a 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -4,5 +4,14 @@

Creado el: <%= l @debate.created_at.to_date %>

+Deja tu comentario +<%= render 'comments/form', parent: @debate %> + +
+

Comentarios

+ <%= render @debate.root_comments %> +
+ +

<%= link_to 'Edit', edit_debate_path(@debate) %> | <%= link_to 'Back', debates_path %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index eae8764ed..cc2da6b4c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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'