AdilA
@AdilA
Нравится кодить, изучаю go c echo

Как сделать древовидные комментарии используя гем acts_as_commentable_with_threading для rails 4?

в общем пытаюсь создать древовидные комментарии, но все тлен...
получилось следующее:
Создана модель Comment.rb как указано в геме

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


Если честно не понял как прикрутить сюда strong parameteres

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>


Теперь когда нажимаю add comment rails ругается на отсутсвие роутов а если включаю js то кнопка не работает.
  • Вопрос задан
  • 3731 просмотр
Решения вопроса 1
AdilA
@AdilA Автор вопроса
Нравится кодить, изучаю go c echo
как ни странно но заработало, добавил @user_who_commented = current_user и изменил
@comment = Comment.build_from(@obj, @user_who_commented.id, @comment_hash[:body])
и заработало
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы