2010-01-23 24 views
2

我有一個簡單的帖子模型,標題爲:字符串和評分:整數,我想添加評分帖子的能力。到目前爲止,我有幫助:在Rails中爲現有帖子模型添加評分

#Post controller 
def increase 
@post = Post.find(params[:id]) 
@post.increment! :rating 
flash[:notice] = "Thanks for your rating." 
redirect_to @post 
end 

#Post show 
<%= link_to "Rating", increase_post_path %> 

#Routes 
map.resources :posts, :member => { :increase => :put } 

當我點擊評分,我得到未知的行動。當我添加@ post.increment時,我可以提高評分! :評級更新,但不是當我創建我自己的方法。任何建議?

回答

1

這是我的解決方案,如果有人感興趣。謝謝你們的幫助。

#Views 
<%= link_to post.rating, increase_post_path(post) %> 

#Controller 
def increase 
    @post = Post.find(params[:id]).increment!(:rating) 
    flash[:notice] = "Thanks for rating" 
    redirect_to posts_url 
end 

#Routes 
map.resources :posts, :member => { :increase => :put } 

如果您只想爲您提供某些東西,那麼這些東西不會被濫用。很顯然,在您的網站上添加評分時,其他人可以無限次地投票,這只是爲了尋求麻煩。

我建議使用Votefu,是否有收視率,甚至有用戶業力。作者非常好,可以製作example app

4

如果你有一個標準的路線說

map.resources :posts 

,你應該將其替換爲:

map.resources :posts, :member => { :increase => :put } 

,這將創建路由「increase_post_path」與法「把」爲您的應用程序。

+0

這就是我做錯了,謝謝。 –