Search code examples
ruby-on-railsrubyparenthesespunditrolify

Rails Rolify with Pundit


The guidance in this post shows how to use rolify with pundit authorisations.

I'm trying to figure out how to this in my Rails app.

What's confusing me about the answer in the linked post is that the index action checks for :admin, where the show action checks for (:admin). Is there a reason for the brackets in some cases?

Why is Pundit not coupled with Rolify like CanCanCan is?

  def index?
    @user.has_role? :admin
  end

  def show?
    @user.has_role?(:admin) || @user.organisation == @organisation
  end

Solution

  • Ruby allows you to leave out parenthesis, it's more like a coding style.

    Normally we omit parenthesis when it comes to methods that are with keyword status in Ruby. (puts, attr_accessor etc.) for the rest methods, we can use ()

    You can learn more on this here: Ruby Style Guide