Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 777
Çağdaş Avatar asked Feb 27 '12 21:02

Çağdaş


1 Answers

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
like image 99
cailinanne Avatar answered Oct 11 '22 15:10

cailinanne