Question

I've defined a custom member_action in ActiveAdmin for one of my models/resources. The main model Controller calls load_and_authorize_resource and ActiveAdmin correctly executes the authorisations that I've defined in my abilities file for that custom member_action.

However, it doesn't make the @my_model resource available in the member_action definition, as I would expect per https://github.com/ryanb/cancan/wiki/authorizing-controller-actions#choosing-actions... It must have been loaded to check the abilities. So why is the resource not available in my action?

I'm running Rails 3.2, CanCan 1.6.10 and ActiveAdmin 0.6.0. Here is the code that I've defined:

/app/controllers/my_models_controller.rb

class MyModelsController < ApplicationController
  load_and_authorize_resource

  ...

end

/app/admin/my_models.rb

ActiveAdmin.register MyModel do
  member_action :dance do
    @my_model.is_dancing = true
    @my_model.save!
    redirect_to admin_my_models_path
end

/app/models/ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    if user and user.is_admin? 
      can :dance, MyModel
    end
  end
end

I can work around this by explicitly loading the instance, but I'd like to understand if I'm missing something or whether it's actually a bug in CanCan + ActiveAdmin.

Was it helpful?

Solution

Have you tried using the resource method provided by inherited_resources?

member_action :dance do
  resource.update_attributes! is_dancing: true
  redirect_to collection_path
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top