【发布时间】:2016-05-02 07:40:58
【问题描述】:
我正在尝试制作一个“更新和删除”按钮,该按钮仅在用户创建帖子或用户或版主 AND/OR 版主时才会显示。
我目前的 posthelper 是
def user_is_authorized_for_post?(post)
current_user && (current_user == post.user || current_user.admin?)
end
我的按钮是:
<% if user_is_authorized_for_post?(@post) %>
<%= link_to "Edit", edit_topic_post_path(@post.topic, @post), class: 'btn btn-success' %>
<%= link_to "Delete Post", [@post.topic, @post], method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this post?' } %>
<% end %>
更新:我将 PostsHelper 更新为:
def user_is_authorized_for_post?(post)
current_user && (current_user == post.user || current_user.admin?) || (current_user == post.user || current_user.moderator?)
end
它有效,但我的问题是,有没有更好的编码方式,因为我觉得它太长了。
【问题讨论】: