responds with ajax to comment creation [#47]
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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 %>
|
||||||
|
|||||||
4
app/views/comments/create.js.erb
Normal file
4
app/views/comments/create.js.erb
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
var form = "#js-comment-form-<%= dom_id(@parent) %>"
|
||||||
|
$(form + " #comment_body").val('');
|
||||||
|
$(form).hide();
|
||||||
|
$("<%= j(render @comment) %>").insertAfter($(form));
|
||||||
Reference in New Issue
Block a user