What is the right way to make new methods available for viewing from the Rails Gem?

If I want to create a new rail pearl that adds methods to Rails views, what is the right way to do this? Extend ActionView :: Base? Will this be related to ApplicationHelper in some way?

+4
source share
1 answer

Many gem authors create a module that defines their helper viewing 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

+6
source

Source: https://habr.com/ru/post/1403062/


All Articles