问题如上,无法让“喜欢”按钮工作,发布更改如下。先感谢您。我修改了路由、模型视图、控制器,如下所示。
充当可投票迁移
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 %>