如何限制用户添加的记录(评论)数量?

发布于 2024-10-29 04:35:44 字数 833 浏览 3 评论 0原文

我怎样才能将用户可以为某个场所撰写的评论数量限制为一条?

如果他们尝试对某个场地进行两次审查,我还希望收到闪现消息提示。

我不太确定应该在问题中包含哪些代码,但这是创建审核方法:

审核控制器

def create
  @review = current_user.reviews.create!(params[:review])
  @review.venue = @venue
  if @review.save
    flash[:notice] = 'Thank you for reviewing this venue!'
    respond_to do |format|
      format.html { redirect_to venue_path(@venue) }
      format.js
    end
  else
    render :action => :new
  end
end

感谢您的任何帮助,非常感谢!

编辑

我已将此辅助方法添加到场地控制器中:

  def reviewed?
    if current_user.reviews.for_venue(@venue).empty?
      true
    else
      false
    end
  end

并将我的表单包装在:

<% if reviewed? %>
  form
<% end %>

但这只是返回 VenuesController 的未定义方法“reviews”

How can I go about restricting the number of reviews a user can write for a venue to just one?

I would also like a flash message prompt if they try to review a venue twice.

I'm not too sure what code I should to include in my question but heres the create review method:

Review controller

def create
  @review = current_user.reviews.create!(params[:review])
  @review.venue = @venue
  if @review.save
    flash[:notice] = 'Thank you for reviewing this venue!'
    respond_to do |format|
      format.html { redirect_to venue_path(@venue) }
      format.js
    end
  else
    render :action => :new
  end
end

Thanks for any help its much appreciated!

edit

I've added this helper method into the venues controller:

  def reviewed?
    if current_user.reviews.for_venue(@venue).empty?
      true
    else
      false
    end
  end

and wrapped my form in:

<% if reviewed? %>
  form
<% end %>

but this just returns undefined method `reviews' for VenuesController

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷雨优美回忆 2024-11-05 04:35:44

如果您能够从一开始就阻止用户查看场地,那就更好了。在您看来(或创建一个助手),请检查该场地是否已被用户审查(有很多方法可以做到这一点)。如果是,请不要出示审核表。简单的。如果您非常 OC,那么您就需要检查控制器。

It would be better if you could prevent the user from reviewing the venue in the first place. In your view(or create a helper) do a check if the venue was already reviewed by the user(many ways to do this). If it was, don't show the review form. Easy. If you're pretty OC, that's when you check in the controller.

灵芸 2024-11-05 04:35:44

也许您应该使用 before_validation 回调,以便您可以检查用户是否已经审核过场地。您可以在创建行中包含场地:

current_user.reviews.create!({:venue_id => @venue.id}.merge(params[:review]))

对于验证,请使用如下内容:

before_validation :check_if_already_reviewed

def check_if_already_reviewed
  if (check if already reviewed)
    return false
  else
    return true
  end
end

Maybe you should use a before_validation callback, so that you can check if the venue has been already reviewed by the user. You can include the venue in the create line:

current_user.reviews.create!({:venue_id => @venue.id}.merge(params[:review]))

And for the validation, use something like this:

before_validation :check_if_already_reviewed

def check_if_already_reviewed
  if (check if already reviewed)
    return false
  else
    return true
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文