2014-02-23 42 views
1

我已經構建了一個簡單的rails應用程序,包含三個模型,Posts,Users和Comments。將線程和電子郵件訂閱添加到評論

我試過每一個評論寶石,他們都有一些不足。

所以我建立了我自己的評論系統。

用戶可以對帖子發表評論。每個評論都是可投票的(使用acts_as_votable gem)。用戶評分由他們收到的總評數組成。

以下是我在我的架構進行了點評:

create_table "comments", force: true do |t| 
    t.text  "body" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "user_id" 
    t.integer "post_id" 
    end 

在我的用戶模型:

class User < ActiveRecord::Base 
    has_many :comments 
    acts_as_voter 
end 

在我的崗位模型:

class Post < ActiveRecord::Base 
    has_many :comments 
end 

在我的評論型號:

class Comment < ActiveRecord::Base 
    belongs_to :post 
    belongs_to :user 
    acts_as_votable 
end 

在我的意見控制器:

class CommentsController < ApplicationController 
    def create 
    post.comments.create(new_comment_params) do |comment| 
     comment.user = current_user 
    end 
    respond_to do |format| 
    format.html {redirect_to post_path(post)} 
    end 
    end 


    def upvote 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.find(params[:id]) 
    @comment.liked_by current_user 

    respond_to do |format| 
     format.html {redirect_to @post} 
    end 
end 


    private 

    def new_comment_params 
    params.require(:comment).permit(:body) 
    end 

    def post 
    @post = Post.find(params[:post_id]) 
    end 

end 

在我的路線文件:

resources :posts do 
    resources :comments do 
    member do 
     put "like", to: "comments#upvote" 
    end 
    end 
    end 

筆者認爲:

<% @post.comments.each do |comment| %> 
    <%= comment.body %> 

    <% if user_signed_in? && (current_user != comment.user) && !(current_user.voted_for? comment) %> 

<%= link_to 「up vote」, like_post_comment_path(@post, comment), method: :put %> 

<%= comment.votes.size %> 

<% else %> 

<%= comment.votes.size %></a> 

<% end %> 
<% end %> 


<br /> 


<%= form_for([@post, @post.comments.build]) do |f| %> 

    <p><%= f.text_area :body, :cols => "80", :rows => "10" %></p> 

    <p><%= f.submit 「comment」 %></p> 

<% end %> 

在用戶配置文件的觀點:(這表明用戶得分

<%= (@user.comments.map{|c| c.votes.count}.inject(:+))%> 

如何實現線程?(在一個級別上,我假設多個級別只是使它真的很亂)

如何使螺紋評論可投票?(雙方父母和子女)什麼必須是完成路線?

如何通過簡單的電子郵件通知用戶可以訂閱接收簡單消息,說明新評論已發佈到他們的主題?

如何獲取用戶評論中收到的所有投票計算的用戶分數,包括用戶評論?

+1

請保持您的問題簡明扼要,並提出一件事。 – phoet

回答

2

如果我理解正確的問題,你想允許註釋評論。在這種情況下,在您的評論模型中,您將需要一個parent_id:integer屬性。然後添加下列關聯:

class Comment 
    ... 
    belongs_to :parent, class_name: 'Comment' 
    has_many :children, class_name: 'Comment', foreign_key: 'parent_id' 
    ... 
end 

現在您有一個樹形結構以供您評論。這允許評論的評論等等。

if my_comment.parent.nil?然後你在根。 if my_comment.children.empty?然後對評論沒有評論。

樹木移動可能很昂貴,因此添加最大深度可能很明智。

1

你如何實現線程? (回答你的其中一個問題)

使註釋 - 用戶關聯變爲多態,然後你可以用同樣的方法添加註釋 - 註釋關聯。

你用現有的寶石發現的「缺陷」是什麼讓你無法做到這一點? (因爲acts_as_commentable支持這個如果框)

+0

我如何將acts_as_commentable與我目前擁有的連接起來? –

+0

有沒有關於如何執行與線程評論一樣的教程?我無法找到如何添加評論的回覆鏈接。 –