MKK
MKK

Reputation: 2753

How can I enable customized action with CanCan?

I have actions called 'follow' and 'unfollow' in my controller.
Obviously, CanCan won't recognize those actions so that it shows access denied when those actions are executed.

alias_action :follow, :unfollow :to => :read

I added this line to ability.rb then it works fine now.
But the problem is when the user is not logged in it shows error like this

syntax error, unexpected ':', expecting keyword_end
    alias_action :follow, :unfollow :to => :read

I only enable those actions when user is logged in.
How can I? What should I add to ability.rb?

Upvotes: 0

Views: 76

Answers (2)

Nerve
Nerve

Reputation: 6851

Assuming your controller to be UsersController, you can do this in your ability.rb fie

def initialize(user)
  user || = User.new
  if user.roles.include?('tweeple')    #Assuming the user with role tweeple can follow/ unfollow
    can [:follow, :unfollow], User
  end
end

Upvotes: 1

davidrac
davidrac

Reputation: 10738

It appears you're missing a comma:

alias_action :follow, :unfollow, :to => :read

see here

Upvotes: 1

Related Questions