[英]how to use acts_as_votable gem
我的應用程序中有三種模型,一種用於發布,報價和用戶,現在我已經建立了一個喜歡用戶可以喜歡發布的喜歡系統,我還沒有使用任何寶石,現在在我使用過的引用模型中添加投票yththe acts_as_votable寶石,用我已經添加了寶石acts_as_voter
在用戶模式和acts_as_votable
引號莫everdel,我運行服務器一切正常,但是當我嘗試喜歡的訊息(不包括引號),我得到我的控制台上的錯誤:
Completed 500 Internal Server Error in 20ms (ActiveRecord: 4.7ms)
NoMethodError (undefined method `vote_by' for nil:NilClass):
app/models/user.rb:64:in `add_like_to'
app/controllers/api/likes_controller.rb:11:in `create'
我已經嘗試了很多方法來解決它,但是沒有任何效果,最后我從用戶模型中刪除了acts_as_voter
,它又開始起作用了,但是對我的報價進行投票是行不通的。
我的用戶模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook, :twitter, :google_oauth2]
validates :username, presence: true
validate :avatar_image_size
has_many :posts, dependent: :destroy
has_many :quotes, dependent: :destroy
has_many :responses, dependent: :destroy
has_many :likes, dependent: :destroy
has_many :liked_posts, through: :likes, source: :likeable, source_type: "Post"
has_many :liked_responses, through: :likes, source: :likeable, source_type: "Response"
has_many :bookmarks, dependent: :destroy
has_many :bookmarked_posts, through: :bookmarks, source: :bookmarkable, source_type: "Post"
has_many :bookmarked_responses, through: :bookmarks, source: :bookmarkable, source_type: "Response"
has_many :notifications, dependent: :destroy, foreign_key: :recipient_id
after_destroy :clear_notifications
after_commit :send_welcome_email, on: [:create]
mount_uploader :avatar, AvatarUploader
include UserFollowing
include TagFollowing
include SearchableUser
include OmniauthableUser
extend FriendlyId
friendly_id :username, use: [ :slugged, :finders ]
def add_like_to(likeable_obj)
likes.where(likeable: likeable_obj).first_or_create
end
def remove_like_from(likeable_obj)
likes.where(likeable: likeable_obj).destroy_all
end
def liked?(likeable_obj)
send("liked_#{downcased_class_name(likeable_obj)}_ids").include?(likeable_obj.id)
end
def add_bookmark_to(bookmarkable_obj)
bookmarks.where(bookmarkable: bookmarkable_obj).first_or_create
end
def remove_bookmark_from(bookmarkable_obj)
bookmarks.where(bookmarkable: bookmarkable_obj).destroy_all
end
def bookmarked?(bookmarkable_obj)
send("bookmarked_#{downcased_class_name(bookmarkable_obj)}_ids").include?(bookmarkable_obj.id)
end
private
# Validates the size on an uploaded image.
def avatar_image_size
if avatar.size > 5.megabytes
errors.add(:avatar, "should be less than 5MB")
end
end
# Returns a string of the objects class name downcased.
def downcased_class_name(obj)
obj.class.to_s.downcase
end
# Clears notifications where deleted user is the actor.
def clear_notifications
Notification.where(actor_id: self.id).destroy_all
end
def send_welcome_email
WelcomeEmailJob.perform_later(self.id)
end
end
我的報價模型
class Quote < ActiveRecord::Base
acts_as_votable
validates :author, presence: true, length: { maximum: 150 }
validates :quote, presence: true, length: { maximum: 300 }, uniqueness: true
is_impressionable
belongs_to :user
def most_significant_word
quote.split.map { |quote| quote.gsub(/\W/, '') }.sort_by(&:length)[-1]
end
end
likes_controller api發布
# This controller serves as a parent controller for other likes_controllers.
# Posts::LikesController for example.
# Child controller that inherit from this LikesController should implement
# before_action :set_likeable, which sets @likeable.
class API::LikesController < ApplicationController
before_action :authenticate_user!
before_action :set_likeable
skip_before_action :verify_authenticity_token
def create
current_user.add_like_to(@likeable)
notify_author
render json: { liked: true, count: @likeable.reload.likes.size, type: @likeable.class.to_s, id: @likeable.id }, status: 200
end
def destroy
current_user.remove_like_from(@likeable)
render json: { liked: false, count: @likeable.reload.likes.size, type: @likeable.class.to_s, id: @likeable.id }, status: 200
end
private
def notify_author
unless current_user?(@likeable.user)
Notification.create(recipient: @likeable.user, actor: current_user, action: "liked your", notifiable: @likeable, is_new: true)
end
end
end
問題是, likes
是acts_as_votable gem中的vote_up_for
的別名方法。 (請參閱此處 )
如果你想同時使用,我建議給足協likes
的別名
has_many :post_likes, class_name: "Like", dependent: :destroy
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.