簡體   English   中英

Rails-如何嵌套注釋-多態

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

對於我的應用程序,我有項目。 我已經使用多態來構建一個名為“ Newcomment”的模型,用於對這些項目的評論。 我遵循了這個軌道 這很好。

但是現在,我想在評論之上構建評論。 我嘗試按照本教程( 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 / )。 我在呈現的每個評論中都添加了一個評論表單。 我還調整了newcomment.rb模型,以便newcomment has_many newcomments和route.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

view / projects / _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>

您只需要https://github.com/elight/acts_as_commentable_with_threading ,而不是從頭開始研究即可。

你不應該有這樣的路線

resources :newcomments do
  resources :newcomments
end

這是難聞的氣味,我們需要修復。 在我們的某些項目中,我們還使用https://github.com/elight/acts_as_commentable_with_threading ,這很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM