简体   繁体   English

Rails-如何嵌套注释-多态

[英]Rails - How Do I Nest Comments - Polymorphism

For my application, I have Projects. 对于我的应用程序,我有项目。 I have used Polymorphism to build a model called "Newcomment" for comments made on these Projects. 我已经使用多态来构建一个名为“ Newcomment”的模型,用于对这些项目的评论。 I followed this railscast . 我遵循了这个轨道 This works great. 这很好。

But now, I want to build comments on top of comments. 但是现在,我想在评论之上构建评论。 I tried following this tutorial ( http://kconrails.com/2010/10/23/nested-comments-in-ruby-on-rails-1-models/ ) and ( http://kconrails.com/2011/01/26/nested-comments-in-ruby-on-rails-controllers-and-views/ ). 我尝试按照本教程( http://kconrails.com/2010/10/23/nested-comments-in-ruby-on-rails-1-models/ )和( http://kconrails.com/2011/01 / 26 / nested-comments-ruby-on-rails-controllers-and-views / )。 I put a form for comments in each comment that I render. 我在呈现的每个评论中都添加了一个评论表单。 I also adjusted the newcomment.rb model, so that newcomment has_many newcomments and the routes.rb file. 我还调整了newcomment.rb模型,以便newcomment has_many newcomments和route.rb文件。

Question: Right now, when I make a comment in the form of each comment, it posts as a comment to the project and not as a response to a specific comment. 问题:现在,当我以每个评论的形式发表评论时,它以项目评论的形式发布,而不是对特定评论的回应。 How would I adjust my code so that I can have comments for comments? 我将如何调整代码,以便对注释发表评论?

newcomment.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 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 routes.rb

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

view/projects/_comments.html.erb view / projects / _comments.html.erb

<%= render @newcomments %>

projects_controller.rb projects_controller.rb

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

view/newcomments/_newcomment.html.erb 查看/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>

您只需要https://github.com/elight/acts_as_commentable_with_threading ,而不是从头开始研究即可。

You should not have the routes like this 你不应该有这样的路线

resources :newcomments do
  resources :newcomments
end

which is a bad smell and we need to fix. 这是难闻的气味,我们需要修复。 In some of our projects, we are also using https://github.com/elight/acts_as_commentable_with_threading and it's good. 在我们的某些项目中,我们还使用https://github.com/elight/acts_as_commentable_with_threading ,这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM