Skip to content
  • Rafael Mendonça França's avatar
    541a51ec
    Implement helpers proxy in controller instance level · 541a51ec
    Rafael Mendonça França authored
    It is a common pattern in the Rails community that when people want to
    :xa
    use any kind of helper that is defined inside app/helpers they includes
    the helper module inside the controller like:
    
        module UserHelper
          def my_user_helper
            # ...
          end
        end
    
        class UsersController < ApplicationController
          include UserHelper
    
          def index
            render inline: my_user_helper
          end
        end
    
    This has problem because the helper can't access anything that is
    defined in the view level context class.
    
    Also all public methods of the helper become available in the controller
    what can lead to undesirable methods being routed and behaving as
    actions.
    
    Also if you helper depends on other helpers or even Action View helpers
    you need to include each one of these dependencies in your controller
    otherwise your helper is not going to work.
    
    We already have a helpers proxy at controller class level but that proxy
    doesn't have access to the instance variables defined in the
    controller.
    
    With this new instance level helper proxy users can reuse helpers in the
    controller without having to include the modules and with access to
    instance variables defined in the controller.
    
        class UsersController < ApplicationController
          def index
            render inline: helpers.my_user_helper
          end
        end
    541a51ec
    Implement helpers proxy in controller instance level
    Rafael Mendonça França authored
    It is a common pattern in the Rails community that when people want to
    :xa
    use any kind of helper that is defined inside app/helpers they includes
    the helper module inside the controller like:
    
        module UserHelper
          def my_user_helper
            # ...
          end
        end
    
        class UsersController < ApplicationController
          include UserHelper
    
          def index
            render inline: my_user_helper
          end
        end
    
    This has problem because the helper can't access anything that is
    defined in the view level context class.
    
    Also all public methods of the helper become available in the controller
    what can lead to undesirable methods being routed and behaving as
    actions.
    
    Also if you helper depends on other helpers or even Action View helpers
    you need to include each one of these dependencies in your controller
    otherwise your helper is not going to work.
    
    We already have a helpers proxy at controller class level but that proxy
    doesn't have access to the instance variables defined in the
    controller.
    
    With this new instance level helper proxy users can reuse helpers in the
    controller without having to include the modules and with access to
    instance variables defined in the controller.
    
        class UsersController < ApplicationController
          def index
            render inline: helpers.my_user_helper
          end
        end
Loading