how to override a gem's views

21 views
Skip to first unread message

fugee ohu

unread,
Jan 5, 2019, 8:33:18 AM1/5/19
to Ruby on Rails: Talk
I'm trying to override the main layout but the gem is actually using other gems together as a group so there's a separate gem for vendor Within the vendor gem can be found app/views but within my main app the question is what should the path to overrides be vendor/app/views or app/views/vendor  

Walter Lee Davis

unread,
Jan 5, 2019, 11:46:45 AM1/5/19
to rubyonra...@googlegroups.com
When a gem is mounted, its internal paths are mounted into the regular Rails lookup paths, but importantly, they are not looked at first in the lookup process. You can read the Rails Guide about Layouts and Rendering to learn more about this process: https://guides.rubyonrails.org/layouts_and_rendering.html#finding-layouts

When a controller in the gem calls "render", explicitly or implicitly, Rails will first look in the app's views directory to find that template or layout or partial. Only if it does not find it there will it look in the gem. I don't think that the vendor directory even comes into play here.

The easiest way to figure this out is to look at the internal layout of the gem on GitHub. Tell us which one you're using, and this can be figured out quite easily. For example, here is a gem that provides views:

https://github.com/rubysherpas/forem

Click into the app/views directory, and you'll see two directories: forem and layouts/forem. These directories are going to be "projected" into the parent Rails app's app/views directory. You won't see them, but Rails will act as though they are there.

If you wanted to override a view provided in either of those directories, say, layouts/forem/default.html.erb, all you would need to do in your app is to create a folder named app/views/forem, and put a file named default.html.erb in it. Whatever you put in that file in your app will be loaded whenever the gem requests the same-named file, and the file that was in the gem itself will be ignored. Rails always loads and uses the first file it finds. This is by design, so an app can override a gem if desired.

Walter

> On Jan 5, 2019, at 8:33 AM, fugee ohu <fuge...@gmail.com> wrote:
>
> I'm trying to override the main layout but the gem is actually using other gems together as a group so there's a separate gem for vendor Within the vendor gem can be found app/views but within my main app the question is what should the path to overrides be vendor/app/views or app/views/vendor
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/608a4abe-c82e-4681-a8ac-0b7996f6b4b2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

fugee ohu

unread,
Jan 7, 2019, 7:50:01 PM1/7/19
to Ruby on Rails: Talk


On Saturday, January 5, 2019 at 8:33:18 AM UTC-5, fugee ohu wrote:
I'm trying to override the main layout but the gem is actually using other gems together as a group so there's a separate gem for vendor Within the vendor gem can be found app/views but within my main app the question is what should the path to overrides be vendor/app/views or app/views/vendor  

Thanks Walter  spree_frontend-3.6.6

Walter Lee Davis

unread,
Jan 7, 2019, 8:35:45 PM1/7/19
to rubyonra...@googlegroups.com
Okay, so you are trying to mount a replacement for something in spree/frontend/app/views/spree/layouts. To shadow over that path from your app, you will want to make a new directory at [your_project]/app/views/spree/layouts. Put whichever replacement layout file you want in that folder, and that's it -- no step three.

Walter

fugee ohu

unread,
Jan 8, 2019, 1:16:25 AM1/8/19
to Ruby on Rails: Talk
Why is frontend omitted from the path ? 

Walter Lee Davis

unread,
Jan 8, 2019, 10:22:22 AM1/8/19
to rubyonra...@googlegroups.com
Look at the paths again. Wherever you find 'app' is the pivot point for the paths. From the point of view of the template lookup system, that's the "you are here". Everything to the left of that will not be present in the lookup. Everything to the right of that is part of the resource path. This same principle works for other lookups, too; it's not restricted to view templates. Controllers, models, helpers -- anything your app needs and a gem might provide -- are all referenced this same way.

Walter

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/786a7cdf-443b-4f7b-973c-531d921a6252%40googlegroups.com.

fugee ohu

unread,
Jan 8, 2019, 6:54:36 PM1/8/19
to Ruby on Rails: Talk
 Thanks
Reply all
Reply to author
Forward
0 new messages