4

If I want to create a new rails gem that adds methods to Rails views what is the right way to do this? Is it to extend ActionView::Base? Would it involve ApplicationHelper in some way?

Inc1982
  • 1,955
  • 1
  • 18
  • 32

1 Answers1

7

Many gem authors create a module that defines their view helper methods and then includes them in ActionView::Base.

module MyGem
  module ActionViewExtensions
    module MyHelpers
      def my_view_helper
        # ...
      end
    end
  end
end
# You can do this here or in a Railtie
ActionView::Base.send :include, MyGem::ActionViewExtensions::MyHelpers

Railtie method:

https://github.com/mynameisrufus/sorted/blob/master/lib/sorted/railtie.rb

Alternative:

https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/action_view_extensions/form_helper.rb

Carlos Ramirez III
  • 7,314
  • 26
  • 33