2015-10-14 104 views
0

我試圖在我的網站上實現喜歡的帖子的可能性,但我沒有這樣做。點擊「喜歡」後,喜歡的數量不會增加。Rails 4.2.0喜歡,像按鈕

這是我的按鈕(部分)。

<%= link_to "Like", like_post_path(post, like: true), method: 'post' %> 
    <%= pluralize(post.likes.size, "like") %> 

而且我的模型:

class Post < ActiveRecord::Base 
    belongs_to :user 
    has_many :likes, as: :likeable 
    ... 
end 

用戶:

has_many :posts, dependent: :destroy 

像:

class Like < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :likeable, polymorphic: true 
end 

像帖子控制器動作:

def like 
    Like.create(user: current_user, like: params[:like]) 
    flash[:success] = "Like Counted!" 
    redirect_to :back 
end 

路線:

resources :posts do 
    member do 
     post 'like' 
    end 
end 

我的遷移: 類CreateLikes <的ActiveRecord ::遷移

def change 
    create_table :likes do |t| 
      t.boolean :like, :default => false 
     t.references :likeable, polymorphic: true 
     t.integer :user_id 

     t.timestamps null: false 
    end 
    end 
end 

PS:我使用多態,因爲我打算加入到這樣的評論,以及能力。

+0

我已經找到答案,這個職位,但決定不刪除它,所以有人可以得到幫助。我簡單地添加了:post = Post.find(params [:id]) Like.create(likeable:post,user:current_user,like:params [:like]) –

+0

您可以將您的評論添加爲答案並「接受」那個答案,以確保你的問題沒有被刪除,其他人可以繼續使用它:) – trh

回答

0

我發現我的問題的答案不久後,我添加此帖子,但決定不刪除它,所以有人可以得到幫助。我簡單地添加:post = Post.find(params [:id])Like.create(likeable:post,user:current_user,like:params [:like])