rails application controller method is undefined in children

1k views Asked by At

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?

1

There are 1 answers

2
Marek Lipka On BEST ANSWER

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