Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails中仅对一个模型限制act_as_votable gem_Ruby On Rails_Acts As Votable - Fatal编程技术网

Ruby on rails 如何在rails中仅对一个模型限制act_as_votable gem

Ruby on rails 如何在rails中仅对一个模型限制act_as_votable gem,ruby-on-rails,acts-as-votable,Ruby On Rails,Acts As Votable,我有一个rails应用程序,其中我有两个表post&Reviews,在post中我添加了一个类似的系统,它是从零开始制作的,但是在Reviews中我想使用gem。我添加了gem,一切正常,但由于两个帖子和评论的用户模型相同,帖子上的like系统已停止工作,因此出现以下错误: NoMethodError (undefined method `vote_by' for nil:NilClass): app/models/user.rb:64:in `add_like_to' app/cont

我有一个rails应用程序,其中我有两个表post&Reviews,在post中我添加了一个类似的系统,它是从零开始制作的,但是在Reviews中我想使用gem。我添加了gem,一切正常,但由于两个帖子和评论的用户模型相同,帖子上的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.rb
中声明
acts\u as\u 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
# 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
class用户[:facebook,:twitter,:google\u oauth2]
验证:用户名,状态:true
验证:化身图像大小
有很多:帖子,依赖::销毁
有很多:引号,依赖::销毁
有很多:响应,依赖::销毁
有很多:喜欢、依赖、破坏
有很多:喜欢的帖子,通过::喜欢,源::喜欢,源类型:“帖子”
通过::likes,source::likeable,source类型:“Response”
有很多:书签,相依::销毁
有很多:带书签的帖子,通过::bookmarks,source::bookmarkable,source类型:“Post”
有很多:书签式的\u响应,通过::bookmarks,source::bookmarkable,source\u类型:“响应”
有多个:通知、依赖::销毁、外键::收件人\u id
销毁后:清除通知
提交后:发送欢迎电子邮件,地址:[:创建]
装载上传者:阿凡达,阿凡达上传者
包括用户跟踪
包括以下标记
包括可搜索用户
包括OmniauthableUser
扩展友好ID
友好的\u id:username,使用:[:sluged,:finders]
def添加喜欢的对象到(喜欢的对象)
喜欢。在哪里(可爱的:可爱的对象)。首先创建
结束
def从(可爱的对象)中删除喜欢的对象
喜欢。在哪里(讨人喜欢:讨人喜欢)。毁掉一切
结束
喜欢什么?(讨人喜欢)
发送(“liked#{downcased_class_name(likeable_obj)}u id”)。包括?(likeable_obj.id)
结束
def添加书签到(可书签对象)
书签。在哪里(可书签:可书签的对象)。首先创建
结束
def从(可书签的对象)中删除书签
书签。在哪里(可书签:可书签)。全部销毁
结束
def已书签?(可书签的_obj)
发送(“书签”{downcased_class_name(bookmarkable_obj)}u id”)。包括?(bookmarkable_obj.id)
结束
私有的
#验证上载图像的大小。
def头像图像大小
如果avatar.size>5.MB
错误。添加(:头像,“应小于5MB”)
结束
结束
#返回对象类名称downcased的字符串。
def downcased_类_名称(obj)
obj.class.to_.downcase
结束
#清除已删除用户为参与者的通知。
def清除通知
通知。where(actor\u id:self.id)。销毁所有
结束
def发送欢迎电子邮件
WelcomeEmailJob.稍后执行(self.id)
结束
结束
喜欢控制器.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
# 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
#此控制器用作其他类似控制器的父控制器。
#Posts::例如LikesController。
#继承自此LikeController的子控制器应实现
#before_action:set_likeable,设置@likeable。
类API::LikeController
在您的
用户
模型中没有调用
投票人
,因此我不确定如何将错误消息粘贴到您的问题中

无论如何,您可能应该在控制器中实现
set\u likable
方法,以便将
@likable
实例变量作为参数传递给
当前用户。将\u like\u添加到(@likable)
已设置


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

错误是告诉您正在对
user.rb
第64行中的nil对象调用
vote\u by
。你能给我们看看你的用户类吗?@FrançoisKoessler现在请看一下,我已经用用户更新了问题。rb@Thatdude错误发生在controller
app/controllers/api/likes\u controlle
中,因此您需要发布控制器代码。@MoamenNaanou在我从user.rb中删除acts\u as\u votable时做了检查。它工作正常,但我使用acts\u as\u votable获得的第二个类似系统无法工作