Rails - 我如何嵌套评论 - 多态性

时间:2013-05-13 22:00:16

标签: ruby-on-rails comments polymorphism

对于我的申请,我有项目。我已经使用Polymorphism来构建一个名为“Newcomment”的模型,用于对这些项目进行评论。我跟着这个railscast。这很有效。

但是现在,我想在评论之上建立评论。我尝试过本教程(http://kconrails.com/2010/10/23/nested-comments-in-ruby-on-rails-1-models/)和(http://kconrails.com/2011/01/26/nested-comments-in-ruby-on-rails-controllers-and-views/)。我在每个评论中添加了一个评论表单。我还调整了newcomment.rb模型,以便newcomment has_many newcomments和routes.rb文件。

问题:现在,当我以每条评论的形式发表评论时,它会作为对项目的评论发布,而不是作为对特定评论的回复。如何调整我的代码以便我可以发表评论意见?

newcomment.rb

class Newcomment < ActiveRecord::Base
  attr_accessible :content, :user_id
  belongs_to :commentable, polymorphic: true
  has_many :newcomments, :as => :commentable

  belongs_to :user

  scope :newest, order("created_at desc")

  validates :content, presence: true
end

newcomments_controller.rb

class NewcommentsController < ApplicationController
  before_filter :load_commentable
  before_filter :authenticate_user!

def create
    @newcomment = @commentable.newcomments.new(params[:newcomment])

    if @newcomment.save
      redirect_to comments_project_path(@commentable), notice: "Comment created."
    else
      render :new
    end
end

def destroy
    if current_user.try(:admin?)
        @newcomment = Newcomment.find(params[:id])
        @commentable = @newcomment.commentable
        @newcomment.destroy

        if @newcomment.destroy
            redirect_to comments_url, notice: "Comment deleted."
        end
    else
        @newcomment = Newcomment.find(params[:id])
        @commentable = @newcomment.commentable
        @newcomment.destroy

        if @newcomment.destroy
           redirect_to comments_project_path(@commentable), notice: "Comment deleted."
        end
    end
end

private
    def load_commentable
            resource, id = request.path.split('/')[1,2]
            @commentable = resource.singularize.classify.constantize.find(id)
    end

end

的routes.rb

resources :projects do
    resources :newcomments do
      resources :newcomments
    end
end

查看/项目/ _comments.html.erb

<%= render @newcomments %>

projects_controller.rb

def comments
    @commentable = @project
    @newcomments = @commentable.newcomments.newest.page(params[:comments_page]).per_page(10)
    @newcomment = Newcomment.new
end

查看/ newcomments / _newcomment.html.erb

<div class="comments"> 
    <%= link_to newcomment.user.name %></strong>
    Posted <%= time_ago_in_words(newcomment.created_at) %> ago
    <%= newcomment.content %>
</div>

<span class="comment">
    <%= form_for [@commentable, @newcomment] do |f| %>
      <div class="field">
        <%= f.text_area :content, rows: 3, :class => "span8" %>
      </div>

      <%= f.hidden_field :user_id, :value => current_user.id %>

      <div class="actions">
        <%= f.submit "Add Comment", :class => "btn btn-header" %>
      </div>

    <% end %>

    <% unless newcomment.newcomments.empty? %>
       <%= render @newcomments %>
    <% end %>
</span>

2 个答案:

答案 0 :(得分:0)

您需要的只是:https://github.com/elight/acts_as_commentable_with_threading

,而不是从废料中处理它

答案 1 :(得分:0)

你不应该有这样的路线

resources :newcomments do
  resources :newcomments
end

这是一种难闻的气味,我们需要修复。在我们的一些项目中,我们也使用https://github.com/elight/acts_as_commentable_with_threading,这很好。

相关问题