如何使用acts_as_votable gem

时间:2018-05-31 11:16:32

标签: ruby-on-rails ruby acts-as-votable

我的应用程序中有三个模型,一个用于帖子,引号和用户,现在我已经建立了一个喜欢系统,用户可以喜欢帖子,我没有使用任何宝石,现在添加投票模型我已经使用yththe acts_as_votable gem,使用我添加的{{1在用户模型和acts_as_voter引号mo everdel中,我运行服务器一切正常,但当我尝试喜欢帖子(不是引号)时,我在控制台上收到此错误:

acts_as_votable

我已经尝试了许多方法来解决它但没有任何效果,最后我从我的用户模型中删除了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
发表

likes_controller api

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

1 个答案:

答案 0 :(得分:0)

问题是likes是acts_as_votable gem中vote_up_for的别名方法。 (见here

如果你想同时使用两者,我建议给关联likes一个别名

has_many :post_likes, class_name: "Like", dependent: :destroy