#269 Template Inheritance
In Rails 3.1 the controller inheritance also applies to the view layer. Here I show how to add an application template which is shared by all views, and a lookup path for overriding templates based on the subdomain.
- source codeProject Files in Zip (106 KB)
- mp4Full Size H.264 Video (14.1 MB)
- m4vSmaller H.264 Video (8.77 MB)
- webmFull Size VP8 Video (10.3 MB)
- ogvFull Size Theora Video (19.7 MB)
Just wondering about last section, what if I wanted to use a layout for each subdomain this way? where to put layouts files?
@Amr: you'd need to create a directory under 'views' for each of the subdomains.. (note that you can decide yourself how to structure your custom view_path, this way you can reflect whatever directory structure you like..)
Hi Ryan, many thanks for the screencast !
I ask you a little clarification: in template inheritance if I put a new link into the application/_side.html.erb partial I'll view it into all children views because they are "extended" but in the episode the view is simple ovverridden (copied and modified), am I right ?
So, is it a "template inheritance" or a "template override" ?
It's "template override", but the controller inherits the template from the parent controller
Kudos! Great screen cast as usual. At 8:20, it should be "over-rode" not "over-rid" :o). Thanks and cheers!
This is awesome!
this + inherited_resources + (simple_form || formtastic) = scaffolding--
As a newbie to Rails this is very exciting, but I am wondering if there is a way to dry this up a little.
With the example, if you copy and override the side partial four times, in four different locations, then that is four places you will need to edit if you make changes to any of the links...
Or is this not a real world practical issue?
I think at that point I would use a combination of techniques - The inherited files could also include a partial of the common elements, and a partial of the added elements - that way you could override only the added elements.
That's really useful thanks
I'm trying to do something similar to the subdomain trick you just showed, but I want to change the controller that's used rather than the view. Is this possible?
I'm making a CMS where a user can create a site and enter their own subdomain. I'd like "/" to point to "public#welcome" if there's no subdomain, but if there is a subdomain, I want it to point to "sites/public#welcome".
Yeah, I'm doing that right now, but it seems like it will become tedious when I have a lot of controllers. Also, how would I define a resource, say "Post", where the controller is at controllers/sites/posts_controller.rb ?
I think I may have found my answer here actually:
...about to try it out now.
I figured it out:
great screencast as usual.
here is my question.
is it possible to give every viewpath custom assets? say i want to create different themes for every shop or every blog in my application. every theme would have different assets (js, css, images) and different layouts and would inherit default behaviour from /views and /public. currently i am using themes_for_rails gem which provides this feature, but a build in solution in rails 3.1 would be great.
Is it possible through template inheritance to make dynamic navigation...
Say that I had a blog application with a list of categories and wanted to link to those categories to show list of articles within those categories, could I use a dynamic method to create the links to the categories show page and place that in an inherited template?
Why did admin namespace not inherit from ApplicationController which inherits from BaseController ? Is the inheritance it limited to a namespace ?
prepend_view_pathis awesome, I wish I had that on a big messy app I picked up some months ago, - thank you
How set the domain name in my application instead of localhost:3000.I'm using ubuntu os.plz tell any gem to set the domain name in my app.
Since prepend_view_path seems to be depreciated in rails 3.2, im looking for the "right" way todo it in 3.2.. any thoughts anyone?
It looks like the deprecation was an empty threat because it's there in 3.2:
<%= controller.view_paths %> dont work. It shows me: #ActionView::PathSet:0x007fe6952365a8
What should I do?
They changed the string array to a PathSet class in Rails 3.2.
To see it as a string array, <%= controller.view_paths.map &:to_path %>