我已經嘗試了每個評論寶石,他們幾乎都吸了。帶有投票和業障的簡單評論
看看這個問題我以前問:Finding user total votes recieved on posts by other users
每推薦下,我決定從頭開始建立自己的評論系統。
這裏是我們的目標:
爲了有一個崗位模型,用戶模型(用色器件),註釋模型。
用戶可以創建評論。這些評論是可投票的。用戶收到的評論總數是他們的評分或業力。
我該如何實現這樣的東西?
到目前爲止,這是我:
我跑
rails generate model Comment commenter:string body:text post:references
遷移
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
在我的評論模型
class Comment < ActiveRecord::Base
belongs_to :post
attr_accessible :commenter, :body
end
在我的崗位模型
class Post < ActiveRecord::Base
has_many :comments
end
我的路線文件
resources :posts do
resources :comments
end
我的評論控制器
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end
end
在我的帖子顯示視圖
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<% end %>
<%= form_for([@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %>
我真的需要幫助這裏:除了選擇一個批評家的名字,我需要控制器來要求t他要用戶登錄,並將當前用戶作爲評論的評論者。我如何實施評論編輯?
然後我需要用這些寶石中的任何一個,使意見可投票:
https://github.com/bouchard/thumbs_up
https://github.com/ryanto/acts_as_votable
最後我需要能夠計算出總票數給定用戶已收到在他們發表的所有評論中。類似於@ user.comments.votes.size
因此,爲了確保我理解正確,您希望'commenter'屬性引用留下評論的用戶,並且您希望它自動分配給'current_user'? –
是的,但我也希望評論是可投票的,並且能夠獲得任何給定用戶在其所有評論中收到的總票數。 –
只是意識到我忘了進入投票總和......將稍微更新我的答案。 –