1

In my Rails 3 application I have a module containing my own helpers which overrides a couple of the built in rails helpers:

module MyHelpers
  def form_for(*args)
     ...
  end

  def link_to(*args)
     ...
  end
end

The above module lives in the lib folder.

I would like these helpers to only be used on specific controller actions. My first stab at this was something like this :

require "my_helpers"
class MyController < ApplicationController 

  before_filter :add_helpers

  def add_helpers
    if some_condition
      ApplicationHelper.send(:include, MyHelpers)
    end
  end
  ..
end

Module.send(:include, ModuleB) is a valid way to include a module within another module, but it doesn't do what I need in this situation : the methods in my module are not available in the view. It seems as though Rails has already determined which helper methods will available to the view before any of the controller methods are run.

Is there a way to do what I need?

I know I could globally override the form_for method but I'd rather not do this.

I think the answer to this would come from understanding how rails makes helper methods accessible to the view.

3
  • why not just rename the methods? they obviously do something else then form_for and link_to are doing.
    – Ben
    Apr 5, 2013 at 9:56
  • Because the library I am developing is doing something that should work with any controller in any rails app. The controller's themselves shouldn't be aware of what is happening.
    – cmrichards
    Apr 5, 2013 at 10:10
  • 1
    I would strongly advise against overriding helper methods native to Rails. Someone new to your code will be thrown when link_to is not working as Rails says it should. As Benjamin suggests, I'd recommend renaming the methods, and updating your views where necessary.
    – osahyoun
    Apr 5, 2013 at 10:26

5 Answers 5

2

config.action_controller.include_all_helpers should be set to false

1
  • this should be set to false by default
    – srghma
    Aug 2, 2018 at 14:06
0

Since Rails automatically loads and includes all the helpers in the app/helpers directory, you could try putting your helpers in a different location, for instance in the lib directory.

This way the .rb-files get parsed and loaded but the module wouldn't be included in the ApplicationHelper class automatically. Then you could do that in your before filter as you stated.

4
  • That's exactly where my customer Helper module lives, in the lib folder. The problem isn't that it is being included automatically, the problem is that I need to include the module only on certain requests.
    – cmrichards
    Apr 5, 2013 at 10:18
  • WHAT doesn't work? Do you get an error message? Did you configure autoload_paths in your config/application.rbcorrectly?
    – Vapire
    Apr 5, 2013 at 10:21
  • There are no error messages, so it does seem to be being included in the ApplicationHelper module. The problem is that the methods in my module are not available to the view.
    – cmrichards
    Apr 5, 2013 at 10:25
  • I'm not sure with this one, but try using ApplicationController.send(:include, MyHelpers) instead of ApplicationHelper
    – Vapire
    Apr 5, 2013 at 10:32
0

Vapire's got a good point about placing these modules somewhere other than app/helpers. I would take it a step further and say what you're trying to do probably isn't wise - having some methods available or not on a per-action basis feels a bit too fragile for my liking.

I would rather take the approach of having custom controller superclasses sitting between your controllers and ApplicationController that include modules (or just have those methods in your superclass).

5
  • That's exactly where my customer Helper module lives, in the lib folder. The problem isn't that it is being included automatically, the problem is that I need to include the module only on certain requests.
    – cmrichards
    Apr 5, 2013 at 10:25
  • I'm suggesting you take an approach that means you're not including modules only on certain requests, but instead include modules just for different controllers (and/or controller superclasses).
    – pat
    Apr 5, 2013 at 10:28
  • Yes but I only need it on certain actions which could be spread amongst several different controllers. I will be using a configuration file to determine which actions (or urls) should be affected.
    – cmrichards
    Apr 5, 2013 at 10:31
  • Fair enough. It's just not an approach I'd take.
    – pat
    Apr 5, 2013 at 10:38
  • I agree - the approach is demented. I was still interested in how to dynamically add helpers though.
    – cmrichards
    Apr 5, 2013 at 10:44
0

Once you add helpers on a controller class or helper class, it will stay loaded after the request. You need unload them after action. Unfortunately there is no official way to unload a part of helpers, you need to write it.

class MyController < ApplicationController
  around_action :load_helper, only: :create

  def load_helper
    self.class.helper_method :your_helper_method
    yield
    self.class.helper { undef your_helper_method }
  end
end
1
  • Looks dangerous when serving multiple threads in the same process...! If only one request is served per process at a time this solution should be fine though.
    – sudoremo
    Jul 31, 2019 at 13:29
-1

The answer is to use the 'helper' class method as follows :

require "my_helpers"
class MyController < ApplicationController 

  before_filter :add_helpers

  def add_helpers
    if some_condition
      MyController.helper MyHelpers
    end
  end
  ..
end
3
  • Do you really need to require "my_helpers"? If the module is in your lib directory this should happen automatically...
    – Vapire
    Apr 5, 2013 at 11:12
  • Yes you do need to require it in rails 3. (unless you add some autoload paths). In Rails 2 it was automatic but now you have to be explicit.
    – cmrichards
    Apr 5, 2013 at 13:21
  • This is not the answer because MyController.helper MyHelpers results the helper loaded on the controller permanently. It delays loading the helper but once loaded it can be called by any other actions.
    – kuboon
    Dec 24, 2015 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.