![](/img/trans.png)
[英]javascript - rails upvoting without act_as_votable gem and no refresh to update
[英]How to limit act_as_votable gem for only one model in rails
我有一個Rails應用,其中有兩個表Posts&Reviews,在Posts中,我添加了一個從頭開始制作的類似系統,但是在Reviews中,我想使用acts_as_votable gem。 我已經添加了gem,並且一切正常,但是由於兩個Posts&Reviews的User模型是相同的,因此Posts上的like系統已停止工作,因此出現以下錯誤:
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'
我認為此錯誤正在發生,因為我已將User聲明為投票人,但在Posts中並不需要。 您能告訴我是否有什么方法可以僅針對一種模型在user.rb
聲明acts_as_voter
嗎?
user.rb
class User < ActiveRecord::Base
acts_as_voter
# 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
likes_controller.rb
# 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 set_likeable
raise NotImplementedError, "This #{self.class} cannot respond to 'set_likeable'"
end
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
您的User
模型中沒有對vote_by
的調用,因此我不確定如何將錯誤消息粘貼到您的問題中。
無論如何,您可能應該在控制器中實現set_likeable
方法,以便設置作為參數傳遞給current_user.add_like_to(@likeable)
的@likeable
實例變量。
我認為這應該可以解決您當前的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.