充当可投票的迁移
class ActsAsVotableMigration < ActiveRecord::Migration
def self.up
create_table :votes do |t|
t.references :votable, :polymorphic => true
t.references :voter, :polymorphic => true
t.boolean :vote_flag
t.string :vote_scope
t.timestamps
end
add_index :votes, [:votable_id, :votable_type]
add_index :votes, [:voter_id, :voter_type]
add_index :votes, [:voter_id, :voter_type, :vote_scope]
add_index :votes, [:votable_id, :votable_type, :vote_scope]
end
def self.down
drop_table :votes
end
end
路线=
resources :users do
resources :posts do
member do
post :like
end
resources :comments
end
end
模型
(User)has_many :posts
(Post)has_many :comments
(Post)belongs_to :user
(Comment)belongs_to :post
控制器(公告)
def like
@post= Post.find(params[:id])
@post.liked_by current_user
redirect_to :back
flash[:notice]="Liked!"
end
指数(公告)
<%= link_to like_user_post_path(post.user,post), :method => :post ,:class =>"" do%>
<span class="btn btn-primary editlike">
<%=post.likes.size %>
<i class="icon-heart ">
</i></span><%end %>
答案 0 :(得分:1)
如果你想制作漂亮的投票系统,你可以使用这些宝石
https://github.com/bouchard/thumbs_up
https://github.com/twitter/activerecord-reputation-system
我在我的应用上使用了thumbs_up并且工作得很好