When building a Rails Gem, how do I get Rails to include my gem's app/views/ in the ActionView paths that it loads when looking for view partials?

[Sorry if this has been asked before I seem to have hit a super-tricky part of rails ActionView… if so please kindly point me in the right direction I’ve been googling but I’m afraid this knowledge might be lost back in 2013…]

FULL SO post here: rubygems - When building a Rails Gem, how do I get Rails to include my gem's app/views/ in the ActionView paths that it loads when looking for view partials? - Stack Overflow

0

I am building a gem.

You can see my super-simple gem here: GitHub - jasonfb/xyz_gem: testing gem/rails loading quirks of gem building — this is experimental only

It’s just a shell.

What I want to know today is what tells Rails that it should load my Gem’s views, that is, the views in app/views/ when loading partials?

I have a rails app called XyzGemTest (not the gem) that uses the gem via a local path include

I have view that I am rendering simply at HellosController for demonstration.

The view renders this:

this is in the app code
<%= render partial: 'hello_world' %>

Please note that my partial hello_world is inside of my Gem-- at app/views/_hello_world.erb (although I tried also moving it to app/views/xyz_gem/_hello_world.erb)

When I load my app, I get this common error:

ActionView::Template::Error (Missing partial hellos/_hello_world, application/_hello_world with {:locale=>[:en], :formats=>[:html], :variants=>, :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:

  • “/Users/jason/Work/CommonCoreJS/XyzGemTest/app/views”
  • “/Users/jason/.rvm/gems/ruby-2.6.4/gems/actiontext-6.0.3.2/app/views”
  • “/Users/jason/.rvm/gems/ruby-2.6.4/gems/actionmailbox-6.0.3.2/app/views” ): 1: this is the wrapper 2: 3: 4: <%= render partial: ‘hello_world’ %>

app/views/hellos/index.erb:4

enter image description here

As you can see, this blank Rails 6.0 app is trying to load view files from presisely 3 places: (1) my own app’s app/views folder (NOT the gem) (2) actiontext (3) actionmailbox

What I’m asking here is what do I need to do to my Gem to make it so that ActionView will include my gem’s app/views/ folder in the ‘chain’ of paths that it looks for?

You are welcome to fork & refactor my Github repo above. which is at

(I know for example that my gem is supposed to have a lib/ folder and this folder must have a file named xyz_gem.rb …… I will add that now and see if it helps)

In your gem structure, have you declared the gem to be an “engine” yet? If not, that could be a fix for this issue. Also, how did you create the gem? Did you use bundler, or did you just try to assemble the parts of the gem separately, by hand, while extracting them from a standing app?

Here’s what this looks like in a gem I wrote:

  # Add features to Rails as an Engine:
  class Engine < Rails::Engine
    isolate_namespace PmacsCardio
    config.autoload_paths << File.expand_path('probes', __dir__)
  end

That’s living in the file pmacs_cardio.rb, in the root of the lib directory. The autoload paths declaration is not necessary for what you are up to, don’t assume that’s important here.

Walter

1 Like