1

I'm having a slight issue.

I have the following controllers set out like so:

class ApplicationController < ActionController::Base
attr_accessor :perms
helper_method :set_permissions

  def set_permissions *permissions
    self.perms = permissions
  end
end

class ApiController < ApplicationController

  set_permissions :api

end

class Api::TokenController < ApiController

  set_permissions :none

end

Rails seems to think set_permissions doesn't exist even though it's clearly there in the inheritance chain (I even set it as a helper method).

The error occurs within TokenController.

Also, I put it as a helper method because, honestly, it wasn't working before and I thought it might fix it.

What am I doing wrong?

p.s. I have tried appending Api:: to the api controller since it's in the api directory but that does nothing.

EDIT: Sorry, wrong controller, I need to pay more attention.

EDIT: ok, so I put the following code in: under the def for set_permissions in the ApplicationController and in the sub controllers:

ap 'ApplicationController responds'
ap self.respond_to?(:set_permissions)

and it responds false for all of them, it's being defined so what's going on?

p.s. ap is awesome_print, just so you know.

EDIT: So, adding self. to def set_permissions and now it's being picked up, but now it's going

undefined method `perms=' for ApiController:Class

EDIT: I changed self. to @ and it fixed the provlem, I Did forget to put into the code that I had an attr_accessor (updated the code to show this. Do controllers not like attr_accessor or somthing?

Thermatix
  • 2,757
  • 21
  • 51

1 Answers1

1

You defined set_permission method as ApplicationController's instance method, while you try to call it as class method. You may need to define it as private class method insead:

class ApplicationController < ActionController::Base
  # ...
  class << self
    private
    def set_permissions(*permissions)
      self.perms = permissions
    end
  end
end
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • That's not really going to work, I need to be able to set different permissions per controller inheritance level (or per controller). That's why I'm trying to use an inherited method to set them per controller without defining the method for each controller. – Thermatix May 20 '14 at 09:40
  • This is pretty much the answer, I basically had to define and use the method like a singleton. – Thermatix May 20 '14 at 10:33