如何限制用户添加的记录(评论)数量?
我怎样才能将用户可以为某个场所撰写的评论数量限制为一条?
如果他们尝试对某个场地进行两次审查,我还希望收到闪现消息提示。
我不太确定应该在问题中包含哪些代码,但这是创建审核方法:
审核控制器
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您能够从一开始就阻止用户查看场地,那就更好了。在您看来(或创建一个助手),请检查该场地是否已被用户审查(有很多方法可以做到这一点)。如果是,请不要出示审核表。简单的。如果您非常 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.
也许您应该使用 before_validation 回调,以便您可以检查用户是否已经审核过场地。您可以在创建行中包含场地:
对于验证,请使用如下内容:
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:
And for the validation, use something like this: