0

我有一个 Rails 应用程序,其中有两个表 Posts 和 Reviews,在 Posts 中我添加了一个从头开始制作的类似系统,但在评论中我想使用acts_as_votable gem。我已经添加了 gem,一切正常,但是因为帖子和评论的用户模型是相同的,帖子上的类似系统已经停止工作,它给出了以下错误:

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_voteruser.rb我的情况下是 Review?

用户.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
4

1 回答 1

0

vote_by您的模型中没有调用,User因此我不确定如何将错误消息粘贴到您的问题中。

无论如何,您可能应该set_likeable在控制器中实现该方法,以便设置@likeable作为参数传递给的实例变量current_user.add_like_to(@likeable)

我认为这应该可以解决您当前的问题。

于 2018-05-29T12:53:31.897 回答