Search code examples
ruby-on-railscancan

Use of CanCan to show part of view to user with a specific role


Rails
CanCan Gem

In the views/books/index.html.slim, view, I have the following:

.form-horizontal-column.autowidth
  .form-group.wtht-label
    = button_to t('download_invoice'), acocunts_receivables_path, class: "btn btn-primary"

But, I only want this button/link to show, if the user's role is cfo

So, in models/ability.rb, I have:

if user.cfo?
  can :manage, Invoice

How do I extend the use of CanCan, to that part of the index view, so only users with cfo role, will see this button?


Solution

  • You can put if statements in 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"