Answer the question
In order to leave comments, you need to log in
How to make tree comments using the acts_as_commentable_with_threading gem for rails 4?
in general, I'm trying to create tree-like comments, but everything is decaying ...
the following turned out:
Comment.rb model was created as indicated in the gem
class Post < ActiveRecord::Base
acts_as_commentable
end
class CommentsController < ApplicationController
def create
@comment_hash = params[:comment]
@obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
# Not implemented: check to see whether the user has permission to create a comment on this object
@comment = Comment.build_from((@obj, current_user, @comment_hash[:body])comment_params)
if @comment.save
render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
else
render :js => "alert('error saving comment');"
end
end
end
private
def comment_params
params.require(:comment).permit.(:commentable_id, :commentable_type, :title, :body, :subject, :user_id, :parent_id, :lft, :rqt)
end
end
class PostsController < ApplicationController
def show
@comments = @post.comment_threads.order('created_at desc')
@new_comment = Comment.build_from(@post, current_user, "")
end
resources :posts, shallow: true do
resources :comments, :only => [:create, :destroy]
end
<%= form_for :comment, :remote => false do |f| %>
<%= f.hidden_field :commentable_id, :value => comment.commentable_id %>
<%= f.hidden_field :commentable_type, :value => comment.commentable_type %>
<%= f.text_area :title, :rows => 10, :cols => 83 %>
<%= f.text_area :body %>
<%= f.submit "add comment" %>
<% end %>
</div>
<div class='comment'>
<hr>
<b><%= comment.user.name %> </b>
<small><%= comment.updated_at%></small> <%= link_to "×",comment_path(comment), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment from # {comment.user.username}?", :disable_with => "×", :class => 'close'%>
<p><%= comment.body %></p>
</div>
</div>
<%= render :partial => "comments/form", :locals => { :comment => @new_comment } %>
<%= render :partial => "comments/comment", :collection => @comments, :as => :comment %>
<div>
Answer the question
In order to leave comments, you need to log in
oddly enough it worked, added @user_who_commented = current_user and changed
@comment = Comment.build_from(@obj, @user_who_commented.id, @comment_hash[:body])
and it worked
https://github.com/elight/acts_as_commentable_with... is it written right there?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question