我有我的应用程序设置,用户可以在其中为电影撰写评论。我想要做的是限制用户为每部电影创建一个评论。我已经成功在我的评语控制器来完成这个像这样:只允许用户为每部电影创建一个评论
class ReviewsController < ApplicationController
before_action :has_reviewed, only [:new]
....
def has_reviewed?
if Review.where(user_id: current_user.id, movie_id: @movie.id).any?
redirect_to movie_reviews_path
flash[:notice] = "You've already written a review for this movie."
end
end
end
如果我现在有麻烦在翻译这个相同的逻辑到我与我的处置制定和CanCanCan的辅助方法索引视图模板。
<% if user_signed_in? && ... %> # current_user has already created a review for this movie
<%= link_to "Edit Review", edit_movie_review_path(@movie, review) %>
<% else %>
<%= link_to "Write a Review", new_movie_review_path %>
<% end %>
另外:有什么办法来提高我的has_reviewed查找?方法?我觉得有更好的方法来编写它,但不能确定最合适的修复方法。
嗨,我有一个类似的例子,但我有点困惑。你有@album或@movie吗? – jedi