ruby on rails 3 - How to apply abilities to a non-restful controller in cancan -


i'm new rails , life of me don't "get" cancan.

i've read this tutorial can't figure out how apply instructions situation.

in cancan wiki there is:

  • an admincontroller
  • a roll_logs action

in ability class says:

can :roll, :logs if user.admin? 

i don't :roll , :logs symbols have controller , action?

all want say, if user admin, give them access admincontroller actions, otherwise don't, possible?

yes possible.

the statement

can :roll, :logs if user.admmin? 

means when calling authorize! :roll, :logs unauthorized exception gets thrown if user isn't admin.

so doesn't have controller or action, untill make so.

if have logs_controller example action roll this.

class logscontroller < applicationcontroller    def roll     authorize! :roll, :logs      # rest of roll functionality.   end 

so in example, want give users admin permission access admin controller actions.

you can achieve this.

ability.rb

class ability   include cancan::ability    def initialize(user)     can(:manage, :admin) if user.admin?   end end 

admin_controller.rb

class admincontroller < applicationcontroller   authorize_resource :class => false   def foo  end   def bar  end  end 

this make sure admins can access foo , bar actions of admin_controller.

the :class => false statement means not authorizing resource, want since not example authorizing post or comment. authorizing actions on controller.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -