Ruby on rails 在视图中隐藏数据库中的重复内容

Ruby on rails 在视图中隐藏数据库中的重复内容,ruby-on-rails,Ruby On Rails,我为我的应用程序创建了一个retweet/repin功能。我应该如何在视图中隐藏重复的“内容”?我在索引视图中设置了它,以便它显示表中的所有条目 我设置了retweet/repin,以便用户B可以重新发布用户A发布的内容。在数据库中,它将从用户A复制原始发布,在“从用户id重新发布”列中,它将显示“内容”的原始id 控制器: class LetsgosController < ApplicationController before_action :correct_user, only

我为我的应用程序创建了一个retweet/repin功能。我应该如何在视图中隐藏重复的“内容”?我在索引视图中设置了它,以便它显示表中的所有条目

我设置了retweet/repin,以便用户B可以重新发布用户A发布的内容。在数据库中,它将从用户A复制原始发布,在“从用户id重新发布”列中,它将显示“内容”的原始id

控制器:

class LetsgosController < ApplicationController
  before_action :correct_user, only: :destroy

  def create
    @letsgo = current_user.letsgos.build(letsgo_params)
    if @letsgo.save
      flash[:success] = "Date posted!"
      redirect_to root_url
    else
      flash[:error] = "Date was not posted!"
      redirect_to root_url
    end
  end

  def destroy
    @letsgo.destroy
    redirect_to root_url
  end

  def index
    @letsgos = Letsgo.all 
    @eat = Letsgo.where(:tag => 'Eat')
  end

  def eatdrink
    @eatdrink = Letsgo.where(:tag => 'Eat/Drink')
  end

  def listenwatch
    @listenwatch = Letsgo.where(:tag => 'Listen/Watch')
  end

  def play
    @play = Letsgo.where(:tag => 'Play')
  end

  def explore
    @explore = Letsgo.where(:tag => 'Explore')
  end

  def other
    @other = Letsgo.where(:tag => 'Other')
  end

  def repost
    @letsgo = Letsgo.find(params[:id]).repost(current_user)
    redirect_to root_url
  end

private

  def letsgo_params
    params.require(:letsgo).permit(:content, :tag)
  end

  def correct_user
    @letsgo = current_user.letsgos.find_by(id: params[:id])
    redirect_to root_url if @letsgo.nil?
  end
end
class LetsgosController“eat”)
结束
脱脂饮料
@eatdreak=Letsgo.where(:tag=>“吃/喝”)
结束
def listenwatch
@listenwatch=Letsgo.where(:tag=>'Listen/Watch')
结束
def播放
@play=Letsgo.where(:tag=>play')
结束
def探索
@explore=Letsgo.where(:tag=>“explore”)
结束
定义其他
@other=Letsgo.where(:tag=>other)
结束
def转售
@letsgo=letsgo.find(params[:id]).repost(当前用户)
将\u重定向到根\u url
结束
私有的
def letsgo_参数
参数require(:letsgo).permit(:content,:tag)
结束
def正确的用户
@letsgo=当前用户.letsgos.find_by(id:params[:id])
如果@letsgo.nil,是否将\u重定向到root\u url?
结束
结束
型号:

class Letsgo < ActiveRecord::Base
  belongs_to :user
  default_scope -> { order('created_at DESC') }
  validates :content, presence: true, length: { maximum: 360 }
  validates :user_id, presence: true

  def repost(user_object)
    new_letsgo = self.dup #duplicates the entire object, except for the ID
    new_letsgo.user_id = user_object.id
    new_letsgo.repost_from_user_id = self.id #save the user id of original repost, to keep track of where it originally came from
    new_letsgo.save
  end

  def is_repost?
    repost_from_user_id.present?
  end

  def original_user
    User.find(repost_from_user_id) if is_repost?
  end
end
class-Letsgo{order('created_at DESC')}
验证:内容,状态:true,长度:{最大值:360}
验证:用户id,状态:true
def repost(用户对象)
new_letsgo=self.dup#复制除ID之外的整个对象
new_letsgo.user_id=user_object.id
new_letsgo.repost_from_user_id=self.id#保存原始repost的用户id,以跟踪其原始来源
新莱茨戈省
结束
def是否重新发布?
是否从用户id处重新发布?
结束
def原始用户
如果是重新发布,则查找(从用户id重新发布)?
结束
结束

如果我理解正确,您不想显示转贴(或原件)。您应该在索引中使用作用域或where方法。
因此,不是:

def index
  @letsgos = Letsgo.all 
  @eat = Letsgo.where(:tag => 'Eat')
end
尝试:

这应该只显示原件。只显示转贴是一个困难的过程,因为过滤它们并不容易。您可能必须在模型中添加一列,以便在重新发布原始列时对其进行标记。比如:

self.reposted = true
self.save

在您的重新发布方法中。

谢谢。我写了一些类似的东西,但不完全是这样。这个答案很基本,所以我应该得出这个结论。谢谢你的帮助!!我只需要出示原件。你帮了大忙
self.reposted = true
self.save