Extending ActionController only partially works in ruby on rails

73 views Asked by At

I have a module which adds some functionality to ActionController::Base. I have included it, and it works for some things. Specifically, UsersController.been_extended returns true. If I override create in the UsersController, form_params also works. The problem is that whenever I try to create a new user, it says that the action create could not be found for UsersController.

module RailsExtender
  module ActionControllerExtension
    extend ActiveSupport::Concern

    def create
      @obj = do_create
      # more stuff here
    end

    private
    def form_params
      fields = self.model.fieldset({ 'create' => 'new', 'update' => 'edit' }.fetch(params[:action], params[:action]))
      params.require(self.model.to_s.to_sym).permit(*fields)
    end

    module ClassMethods
      def been_extended  # just for testing purposes
        true
      end
    end
  end
end

ActionController::Base.send(:include, ActionControllerExtension)

And this is my controller which I'm trying to call create on.

module Auth
  class UsersController < ApplicationController
    @model = User
  end
end

Why doesn't it think create exists?

1

There are 1 answers

0
Matt Brictson On BEST ANSWER

Instead of monkey patching it into ActionController::Base like you do here:

ActionController::Base.send(:include, ActionControllerExtension)

Mix it into your ApplicationController like this:

class ApplicationController < ActionController::Base
  include ActionControllerExtension
  # ...
end

My suspicion is that ActionController::Base is blacklisting all of its own methods for security reasons. Only subclasses of ActionController::Base (i.e. your ApplicationController, etc.) can define actions.