0

我正在尝试在我的 Rails 应用程序中使用acts_as_votable。目前我有一个链接,允许用户喜欢或不喜欢帖子。但现在它总是显示不喜欢的链接( current_user 喜欢状态)。甚至在我为数据库播种时也是如此。

应用程序/控制器/status_controller.rb

class StatusController < ApplicationController
  before_action :set_status

  def like
    current_user.liked_by @status
    respond_to do |format|
      format.js {render inline: "location.reload();" }
    end
  end

  def dislike
    current_user.unliked_by @status
    respond_to do |format|
      format.js {render inline: "location.reload();" }
    end
  end

  private

  def set_status
    @status = Status.find(params[:id])
  end

应用程序/模型/user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable, :lockable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :statuses, dependent: :destroy
  mount_uploader :images, ImageUploader
  mount_uploader :avatar, AvatarUploader
  acts_as_voter

  def age
    Time.now.year - birth_date.year
  end

  def name
    "#{first_name} #{last_name}".titleize
  end
end

应用程序/模型/status.rb

class Status < ActiveRecord::Base
  acts_as_votable
  belongs_to :user
end

app/views/welcome/home.html.slim

- @statuses.each do |status|
  .chip
    = cl_image_tag status.user.avatar.full_public_id, alt: status.user.name
    = " #{status.user.name} #{status.user.age}, #{status.user.city}, #{status.user.state}"
  p
    = status[:message]
  p
    - if current_user.voted_up_on? status
      = link_to dislike_status_path(status), method: :post, remote: true do
        i.small.material-icons.blue-text
          | thumb_up
    - else
      = link_to like_status_path(status), method: :post, remote: true  do
        i.small.material-icons.grey-text
          | thumb_up
    = time_ago(status.created_at)
4

1 回答 1

1

看来你喜欢/不喜欢错误的方式

  def like
    current_user.liked_by @status

  def dislike
    current_user.unliked_by @status

但是你必须写@status.liked_by current_user@status.unliked_by current_user

检查文档和示例

@post.liked_by @user1
@post.unliked_by @user1
于 2015-11-02T09:25:34.053 回答