【问题标题】:Blacklist model in Rails 4Rails 4 中的黑名单模型
【发布时间】:2014-11-22 20:33:37
【问题描述】:

我正在使用 Act_as_votable 来实现 like/dislike 投票系统。它工作得非常完美。

但现在我面临将至少收到 30 票反对的项目列入黑名单的问题。

我有模特广告。这有 in_blacklist 列,默认值为 false。 我在广告控制器中添加了 in_blacklist 字段。 到目前为止我已经尝试过了。

在视图中:

<%= link_to "Like", like_advertisement_path(@advertisement), method: :put %> <%= @advertisement.get_likes.size %>)
(<%= link_to "Dislike", dislike_advertisement_path(@advertisement), method: :put %> <%= @advertisement.get_dislikes.size %>)

在控制器中:

def downvote
  @advertisement.downvote_from current_user
flash[:notice] = 'Downvote added.' if @advertisement.vote_registered?
  if @advertisement.get_dislikes.size ==  30

    @advertisement.in_blacklist = true
     flash[:notice] = "#{@advertisement.name } added to blacklist. Information sent to #{@advertisement.user.email } "
    respond_with(@advertisement)
  else
  respond_with(@advertisement)
  end
end

所以当我第 30 次不喜欢时,没有错误消息。当我检查布尔值是否发生变化时,它仍然是错误的。

【问题讨论】:

    标签: ruby-on-rails blacklist voting-system


    【解决方案1】:

    你忘记保存对象了。

    def downvote
      @advertisement.downvote_from current_user
    flash[:notice] = 'Downvote added.' if @advertisement.vote_registered?
      if @advertisement.get_dislikes.size ==  30
    
        @advertisement.in_blacklist = true
        @advertisement.save # there you go!
         flash[:notice] = "#{@advertisement.name } added to blacklist. Information sent to #{@advertisement.user.email } "
        respond_with(@advertisement)
      else
      respond_with(@advertisement)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多