我很难搞清楚这一点。在我的索引页面上,我有一个隐藏投票卡的开关。 kaminari分页宝石的工作方式,如果你已经投了前10张牌,第1页的显示结果将少于10张牌。所以我试图删除用户投票的卡片,如果参数存在。来自阵列的Rails阵列拒绝
如何设置@ cards.reject以处理要删除的项目数组?
cards_controller.rb
@cards = Card.all
@card_total = @cards.count
@three_pics = @cards.sample(3, random: 1)
@new_cards = @cards.last(5).reverse!
if params[:voted] == 'on' then
@voted = Vote.where(:user_id => current_user.id).select(:card_id)
@cards = @cards.reject { |item| block }
end
if params[:view] == 'new' then
@cards = @cards.order(created_at: :desc).page(params[:limit]) if params[:limit]
elsif params[:view] == 'old' then
@cards = @cards.order(created_at: :asc)
elsif params[:view] == 'least_pop' then
@cards = @cards.order(popularity: :asc, created_at: :asc)
else
@cards = @cards.order(popularity: :desc, created_at: :desc)
end
更新
让我用一个例子和一些更新的代码
@voted_did = Vote.where(:user_id => current_user.id).pluck(:card_id)
@voted = [1,3阐述, 8]
@cards = [{“id”=>“1”,“event”=>“Some Event”,{“id”=>“2”,“event”=>“Some Event”,... ...},{“id”=>“3”,“event”=>“Some Event”,........},{“id”=>“4”,“event” =>“Some Event”,........},{“id”=>“5”,“event”=>“Some Event”,........},{“id “=>”5“,”event“=>”某事件“,........},{”id“=>”7“,”事件“=>”某事件“,... ...},{“id”=>“6”,“event”=>“一些事件”,........},{“id”=>“9”,“event” =>“一些事件”,........}}
我想弄清楚如何从@cards中删除id,但是如果找到该id的整个数组in @voted
感谢cards.rejected上@尖,我还是错过了一些艰难的事情。我认为拉动用户没有投下的所有选票来取消他们投票的卡将是一个漫长的过程。 – Ian 2014-10-19 03:30:37
所以部分块应该包含你的逻辑,可以是这样的: @ cards.reject {| item | @ voted.include?(item [:id])} – pazinjanka 2014-10-19 08:19:27