当前,我正在创建一个Rails应用程序以了解框架,现在我的控制器对用户进行身份验证,我使用enum拥有三种类型的用户,并且在users表中有一个名为row的字段,在我的应用程序控制器上我拥有此>
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
protected
def authenticate_editor!
redirect_to request.referrer, :flash => { :error => "You don't have
the permissions to do this!" } unless user_signed_in? &&
current_user.editor?
end
def authenticate_admin!
redirect_to request.referrer, :flash => { :error => "You don't have
the permissions to do this!" } unless user_signed_in? &&
current_user.admin?
end
end
因此,现在在我的控制器上,我为那些受保护的方法定义了一个先行动作,例如,admin角色是唯一无法销毁我的posts_controller中的帖子的角色。那行得通,但在我看来,任何用户都可以在该模型的索引页上看到破坏动作链接,如果普通用户单击该链接,就会出现闪烁,并且他无法完成该动作,但是我不知道不想让普通用户看到对他们无用的链接,我知道我可以例如使用以下方法隐藏它:
<% if current_user.admin? %>
<%= link_to 'Destroy', post_path(post),
method: :delete,
data: { confirm: 'Are you sure?' } %>
<% end %>
但是我想知道这是否是正确的方法,是否应该继续使用有条件的操作链接或是否有更好的方法?