How to extract Rails view helpers into a gem?

I have a set of rails for viewing helpers, which I use regularly, and would like to pack them in a gem so that I can just put a string in my Gemfile and access the helpers from my views.

I created the gems before using the Bundler and Jeweler, however I donโ€™t quite understand how to organize the Rails view helpers in the gem and put them on the rails.

I would appreciate any pointers or links to modern tutorials on how to do this for Rails 3

thank

Just to clarify: the question is not "how to create a gem." His "how to pack view helpers in jewelry, so I can use them in Rails"

Edit 2: I also agree with the poster below. The rail engine is too heavily overloaded for this kind of (hopefully simple) requirement

+53
ruby-on-rails ruby-on-rails-3 rubygems gem actionview
Apr 26 2018-11-11T00:
source share
3 answers

In my opinion, a complete Engine is redundant for this task. Instead, you can simply create a Railtie that includes your helpers in the ActionView :: Base during initialization.

# lib/my_gem/view_helpers.rb module MyGem module ViewHelpers def pre(text) content_tag :pre, text end def another_helper # super secret stuff end end end # lib/my_gem/railtie.rb require 'my_gem/view_helpers' module MyGem class Railtie < Rails::Railtie initializer "my_gem.view_helpers" do ActionView::Base.send :include, ViewHelpers end end end # lib/my_gem.rb require 'my_gem/railtie' if defined?(Rails) 
+88
Apr 26 '11 at 19:52
source share

Also, if you want to enable the helper only for the Rails3 version, you can use

 # lib/my_gem.rb require 'my_gem/railtie' if defined?(Rails::Railtie) 
+5
Jun 27 '12 at 16:00
source share

What you are probably looking for is an engine . An engine is a jewel that contains pieces of rails (in fact, the application of rails is itself an engine.)

+2
Apr 26 '11 at 19:41
source share



All Articles