D
D
dexdev2013-12-11 06:08:00
Ruby on Rails
dexdev, 2013-12-11 06:08:00

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

To be honest, I didn’t understand how to tie strong parameteres here
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

view/comments/_form.html.erb
<%= 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>

view/comments/_comment.html.erb
<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>

and views/posts/show.html.erb
</div>
          <%= render :partial => "comments/form", :locals => { :comment => @new_comment } %>
          <%= render :partial => "comments/comment", :collection => @comments, :as => :comment %>
            <div>

Now when I click add comment rails swears at the lack of routes, and if I include js, the button does not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dexdev, 2013-12-11
@AdilA

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

A
Alexey Kolosov, 2013-12-11
@satisFUCKtor

https://github.com/elight/acts_as_commentable_with... is it written right there?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question