2014-02-14 28 views
0

我已經嘗試了每個評論寶石,他們幾乎都吸了。帶有投票和業障的簡單評論

看看這個問題我以前問: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

+1

因此,爲了確保我理解正確,您希望'commenter'屬性引用留下評論的用戶,並且您希望它自動分配給'current_user'? –

+0

是的,但我也希望評論是可投票的,並且能夠獲得任何給定用戶在其所有評論中收到的總票數。 –

+1

只是意識到我忘了進入投票總和......將稍微更新我的答案。 –

回答

1

要處理將current_user分配給註釋,首先需要將commenter列更改爲引用用戶的ID(我也將其重命名爲commenter_id)。所以,你要產生像這樣的模式:

rails generate migration ChangeCommentsCommenterToCommenterId 

# db/migrate/<timestamp>_change_comments_commenter_to_commenter_id.rb 
class ChangeCommentsCommenterToCommenterId < ActiveRecord::Migration 
    def change 
    remove_column :comments, :commenter 
    add_column :comments, :commenter_id, :integer, null: false 
    add_index :comments, :commenter_id 
    end 
end 

或者,重新從頭模型:

rails generate model Comment commenter_id:integer:index body:text post:references 

請注意,我添加一個索引列。在您Comment型號:

# app/models/comment.rb 
class Comment < ActiveRecord::Base 
    belongs_to :post 
    belongs_to :commenter, class_name: 'User' 
    attr_accessible :body 
end 

需要注意的是,因爲我們使用的belongs_to在這裏,當你發送commenter消息的Comment一個實例,你會回來的User一個實例。

接下來,您需要更新控制器以進行正確的用戶分配。我還建議有點重構私有方法來使實現更具表現力的域:

# app/controllers/comments_controller.rb 
class CommentsController < ApplicationController 
    def create 
    post.comments.create(new_comment_params) do |comment| 
     comment.commenter = current_user 
    end 
    redirect_to post_path(post) 
    end 

    private 

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

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

既然你重定向到post_path,我認爲你不需要保持@comment實例變量。

最後,您需要從視圖中的表單中移除評論者字段。這有訣竅嗎?

[編輯:添加本節,以解決有關選舉的問題...]

我不是正是你要尋找的幫助投票的部分完全清楚,但是從最低高層次的角度來看,我猜你可能會想要一個VotesController這是從嵌套的路徑訪問:

# config/routes.rb 
... 
resources :comments do 
    resources :votes, except: :index 
end 

希望這有助於!

+0

謝謝sooooooo多!添加內聯編輯有多難?所以他們可以點擊並編輯鏈接,使評論文本可編輯? –

+1

這裏有一個[Railscast](http://railscasts.com/episodes/302-in-place-editing)。這有點過時,但應該給你一些理解如何實現它的良好勢頭。 –

+0

謝謝!如果你有機會可以看看這個,我沒有收到選票發生的路由錯誤。 (我刪除了評論者,並直接將用戶鏈接到數據庫中的註釋,該註釋正在工作):http://stackoverflow.com/questions/21801836/routes-on-comments-and-votes –