Search code examples
ruby-on-railsrubycancan

Passing params to CanCan in RoR


I have a controller with a method like;

def show

    if params[:format].eql?("pdf")
    // do something
    elsif params[:format].eql?("csv")
    // do something
    end
end

But i have users with different roles. So i use CanCan to manage access control. Now i want X role can do the action show in controller iff params[:format].eql?("csv")

I think it can be like ;can :show, resource if params[:format].eql?("csv"). So how can i send parameters to ability.rb?

Any idea?

Thanks.


Solution

  • In ApplicationController add the following:

    # CanCan - pass params in to Ability
    # https://github.com/ryanb/cancan/issues/133
    def current_ability
      @current_ability ||= Ability.new(current_user, params)
    end