<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>template inheritance « Plataformatec Blog</title>
	<atom:link href="/tag/template-inheritance/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Plataformatec&#039;s place to talk about Ruby, Ruby on Rails, Elixir, and software engineering</description>
	<lastBuildDate>Mon, 04 Apr 2011 20:39:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.2</generator>
	<item>
		<title>Default views in Rails 3.0 with custom resolvers</title>
		<link>/2011/04/default-views-in-rails-3-0-with-custom-resolvers/</link>
					<comments>/2011/04/default-views-in-rails-3-0-with-custom-resolvers/#comments</comments>
		
		<dc:creator><![CDATA[José Valim]]></dc:creator>
		<pubDate>Mon, 04 Apr 2011 18:00:26 +0000</pubDate>
				<category><![CDATA[English]]></category>
		<category><![CDATA[crafting rails applications]]></category>
		<category><![CDATA[rails 3]]></category>
		<category><![CDATA[rails 3.1]]></category>
		<category><![CDATA[resolvers]]></category>
		<category><![CDATA[template inheritance]]></category>
		<guid isPermaLink="false">/?p=1959</guid>

					<description><![CDATA[<p>It is common in Rails 3.0 applications that you want to provide default views for a group of controllers. Let&#8217;s say you have a bunch of controllers inside the Admin namespace and you would like each action to fallback to a default template. So if you are rendering the index action for Admin::PostsController and &#8220;app/views/admin/posts/index.html.*&#8221; ... <a class="read-more-link" href="/2011/04/default-views-in-rails-3-0-with-custom-resolvers/">»</a></p>
<p>The post <a href="/2011/04/default-views-in-rails-3-0-with-custom-resolvers/">Default views in Rails 3.0 with custom resolvers</a> first appeared on <a href="/">Plataformatec Blog</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>It is common in Rails 3.0 applications that you want to provide default views for a group of controllers. Let&#8217;s say you have a bunch of controllers inside the <code>Admin</code> namespace and you would like each action to fallback to a default template. So if you are rendering the index action for <code>Admin::PostsController</code> and &#8220;app/views/admin/posts/index.html.*&#8221; is not available, it should then render &#8220;app/views/admin/defaults/index.html&#8221;.</p>
<p>There are several ways to implement this feature at the controller level. It mainly relies on trying to render the original template and then rescue <code>ActionView::MissingTemplate</code>. If this error is rescued, you then render the default one. However, there is a considerable performance overhead in this approach as it needs to pass through the rendering and template lookup stack twice.</p>
<p>Luckily, since Rails 3.0, we have a new abstraction called <strong>resolvers</strong> that holds the logic to find a template. I explain comprehensively how resolvers work and their API in my book <a href="http://plataformatec.com.br/crafting-rails-applications/" target="_blank">Crafting Rails Applications</a>. So here I would just show the basics to get this functionality working.<br />
First, we need to define a <code>DefaultResolver</code>, it could be done inside the lib directory:</p>
<pre lang="ruby">
class MyResolver < ::ActionView::FileSystemResolver
  def initialize
    super("app/views")
  end

  def find_templates(name, prefix, partial, details)
    super(name, "admin/defaults", partial, details)
  end
end
</pre>
<p>Our new resolver simply inherits from <code>ActionView::FileSystemResolver</code> and does two changes: Overrides the <code>initialize</code> method so the view path defaults to "app/views" inside our application and overrides <code>find_templates</code>. The <code>find_templates</code> method receives the template name, a prefix (i.e. the controller path), a boolean marking if the template is a partial or not and a hash of details. In the example above, we simply ignore the prefix given and hardcode it to "admin/defaults".</p>
<p>Now, assuming that all controllers inside the Admin namespace inherit from an <code>Admin::ApplicationController</code>, we can add default views to all of them by adding the following line:</p>
<pre lang="ruby">
class Admin::ApplicationController < ActionController::Base
  append_view_path MyResolver.new
end
</pre>
<p>And we are done! The <code>view_paths</code> holds a list of paths and/or resolvers that the controller will look for templates until one is found. If none is found, an <code>ActionView::MissingTemplate</code> is raised. Since we used <code>append_view_paths</code>, our resolver was added after the "app/views" path, used by default in all controllers.</p>
<p>As you may have guessed, resolvers are a powerful abstraction that allows you to retrieve templates from anywhere, including the database, which is the example given in <a href="http://plataformatec.com.br/crafting-rails-applications/" target="_blank">Crafting Rails Applications</a>.</p>
<p>Finally, template inheritance was a feature recently added to Rails master (upcoming Rails 3.1), so you won't need to create your custom resolver as above. <a href="http://edgerails.info/articles/what-s-new-in-edge-rails/2011/01/12/template-inheritance/index.html">There is a good wrap up about this feature in Rails Edge</a>.</p><p>The post <a href="/2011/04/default-views-in-rails-3-0-with-custom-resolvers/">Default views in Rails 3.0 with custom resolvers</a> first appeared on <a href="/">Plataformatec Blog</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>/2011/04/default-views-in-rails-3-0-with-custom-resolvers/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
