使用 CanCan 向具有特定角色的用户显示部分视图
Use of CanCan to show part of view to user with a specific role
Rails
CanCan Gem
在 views/books/index.html.slim 视图中,我有以下内容:
.form-horizontal-column.autowidth
.form-group.wtht-label
= button_to t('download_invoice'), acocunts_receivables_path, class: "btn btn-primary"
但是,如果用户的角色是 cfo
,我只想显示这个 button/link
因此,在 models/ability.rb 中,我有:
if user.cfo?
can :manage, Invoice
如何将 CanCan 的使用扩展到索引视图的那部分,以便只有具有 cfo 角色的用户才能看到此按钮?
您可以在 haml
.
中放置 if 语句
.form-horizontal-column.autowidth
.form-group.wtht-label
- if can? :manage, Invoice
= button_to t('download_invoice'), acocunts_receivables_path, class: "btn btn-primary"
Rails
CanCan Gem
在 views/books/index.html.slim 视图中,我有以下内容:
.form-horizontal-column.autowidth
.form-group.wtht-label
= button_to t('download_invoice'), acocunts_receivables_path, class: "btn btn-primary"
但是,如果用户的角色是 cfo
,我只想显示这个 button/link因此,在 models/ability.rb 中,我有:
if user.cfo?
can :manage, Invoice
如何将 CanCan 的使用扩展到索引视图的那部分,以便只有具有 cfo 角色的用户才能看到此按钮?
您可以在 haml
.
.form-horizontal-column.autowidth
.form-group.wtht-label
- if can? :manage, Invoice
= button_to t('download_invoice'), acocunts_receivables_path, class: "btn btn-primary"