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.
can
takes two arguments: first is type of action that user is trying to perform on a resource, second is resource (can be class name or instance variable) itself. If you have your Ability set correctly, you should be able to do something like this:Don't forget that you have to have your user authenticated before running any CanCan checks.
can?
method only returns true or false. I normally like to useauthorize!
method to check abilities. Unlikecan
, it would riseCanCan::AccessDenied
error that you can rescue and process gracefully. Something in the lines of:Then, I just catch the exception on ApplicationController level.
The most current answer is in the CanCan wiki: https://github.com/ryanb/cancan/wiki/Accessing-Request-Data
In ApplicationController add the following: