responds with ajax to comment creation [#47]

This commit is contained in:
rgarcia
2015-07-28 20:32:53 +02:00
parent 6a6804f73b
commit 3247180dfa
3 changed files with 10 additions and 5 deletions

View File

@@ -1,12 +1,13 @@
class CommentsController < ApplicationController class CommentsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
before_action :set_debate, :set_parent before_action :set_debate, :set_parent
respond_to :html, :js
def create def create
comment = Comment.build(@debate, current_user, params[:comment][:body]) @comment = Comment.build(@debate, current_user, params[:comment][:body])
comment.save! @comment.save!
comment.move_to_child_of(@parent) if reply? @comment.move_to_child_of(@parent) if reply?
redirect_to @debate, notice: "Comentario guardado" respond_with @comment
end end
private private

View File

@@ -1,7 +1,7 @@
<%= link_to comment_link_text(parent), "", class: "js-add-comment-link", data: {'id': dom_id(parent)} %> <%= link_to comment_link_text(parent), "", class: "js-add-comment-link", data: {'id': dom_id(parent)} %>
<div id="js-comment-form-<%= dom_id(parent) %>" style="display:none"> <div id="js-comment-form-<%= dom_id(parent) %>" style="display:none">
<%= form_for [@debate, Comment.new] do |f| %> <%= form_for [@debate, Comment.new], remote: true do |f| %>
<%= f.text_area :body %> <%= f.text_area :body %>
<%= f.hidden_field :commentable_type, value: parent.class %> <%= f.hidden_field :commentable_type, value: parent.class %>
<%= f.hidden_field :commentable_id, value: parent.id %> <%= f.hidden_field :commentable_id, value: parent.id %>

View File

@@ -0,0 +1,4 @@
var form = "#js-comment-form-<%= dom_id(@parent) %>"
$(form + " #comment_body").val('');
$(form).hide();
$("<%= j(render @comment) %>").insertAfter($(form));