<?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/"
	>

<channel>
	<title>xambr::blog &#187; Tutorials</title>
	<atom:link href="http://blog.xambr.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xambr.com</link>
	<description>crafting my thoughts</description>
	<lastBuildDate>Tue, 20 Jul 2010 18:57:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Ramaze Error Handling</title>
		<link>http://blog.xambr.com/2010/07/19/ramaze-error-handling/</link>
		<comments>http://blog.xambr.com/2010/07/19/ramaze-error-handling/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 02:39:37 +0000</pubDate>
		<dc:creator>max.rb</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[handling 404]]></category>
		<category><![CDATA[ramaze]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.xambr.com/?p=281</guid>
		<description><![CDATA[I recently had to build custom 404 &#38; exception handlers for a Ramaze application. There are several useful discussions on the Ramaze Google Group regarding this topic but I thought I&#8217;d synthesize the solution here for the sake of convenience. Handling 404 Errors Ramaze has a built-in controller action to handle 404 errors; it presents a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to build custom 404 &amp; exception handlers for a Ramaze application. There are several useful discussions on the <a href="http://groups.google.com/group/ramaze" target="_blank">Ramaze Google Group</a> regarding this topic but I thought I&#8217;d synthesize the solution here for the sake of convenience.</p>
<h2>Handling 404 Errors</h2>
<p><a href="http://ramaze.net/" target="_blank">Ramaze</a> has a built-in controller action to handle 404 errors; it presents a vanilla page stating &#8216;No action found at: <code>/xyz</code>&#8216;. Overriding this action is easy and <a href="http://doc.rubyists.com/ramaze%2binnate/Innate/Node.html#action_missing-instance_method" target="_blank">well documented</a> but there are a couple of things that might want to consider.</p>
<p>First, if you would like all 404 errors across your application to be handled by a single action, you will need to add your handling code to the base controller.</p>
<p>Second, using a view file corresponding to the controller action does not quite work. While the view is correctly rendered when the 404 occurs directly under a path mapped to the 404 handler&#8217;s controller (eg. <code>/my_missing_action</code>) it is not rendered for 404 errors occurring under paths mapped to other controllers (eg. <code>/users/my_missing_action</code>).</p>
<p>A workaround for this is to use the <code><a href="http://doc.rubyists.com/ramaze%2binnate/Innate/Helper/Render.html#render_file-instance_method" target="_blank">render_file</a></code> method to manually render a view you&#8217;ve created. I have not done due diligence on this problem so I cannot tell you why it occurs &#8211; maybe it is a bug or maybe (likely) I&#8217;m missing something. If anyone has an explanation or a better work around I&#8217;d love to hear it.</p>
<h3>Source for a Custom 404 Handler</h3>
<p><script src="http://gist.github.com/482339.js"> </script></p>
<h2>Handling Exceptions</h2>
<h3>Routing Exceptions</h3>
<p><a href="http://ramaze.net/" target="_blank">Ramaze</a> also has a built-in exception handler which presents a developer-friendly stack trace (one which I&#8217;m sure you will want to hide for the end-users). The <a href="http://groups.google.com/group/ramaze" target="_blank">Ramaze Google Group</a> discussions <a href="http://groups.google.com/group/ramaze/browse_thread/thread/7d90b3f24b80d01e/c92b7eaae3ab73b3?lnk=gst&amp;q=exception+handling#c92b7eaae3ab73b3" target="_blank">here</a> and <a href="http://groups.google.com/group/ramaze/browse_thread/thread/66f7246c29b1a20c/8d0fc5300ebdc34f?lnk=gst&amp;q=exception+handling#8d0fc5300ebdc34f" target="_blank">here</a> talk about how to override this default handler. It boils down to using <code><a href="http://doc.rubyists.com/ramaze%2binnate/Rack/RouteExceptions.html#route-class_method" target="_blank">Rack::RouteExceptions.route(exception, to)</a>;</code> a method that takes the type of exception to route and the path of the action to route it to.</p>
<p>For example, to route all exceptions to the same controller you could use (as stated by Lars Olsson <a href="http://groups.google.com/group/ramaze/browse_thread/thread/7d90b3f24b80d01e/c92b7eaae3ab73b3?lnk=gst&amp;q=exception+handling#c92b7eaae3ab73b3" target="_blank">here</a>):</p>
<pre>Rack::RouteExceptions.route(Exception, MyController.r(:error))</pre>
<p>Alternatively, to handle all IOError exceptions separately you could use:</p>
<pre>Rack::RouteExceptions.route(IOError, MyController.r(:io_error))
Rack::RouteExceptions.route(Exception, MyController.r(:error))</pre>
<p>Note the order of the calls to route is important. You must route the most specific exceptions first or they will be routed to more general handlers specified before them.</p>
<p>Unlike the 404 error handler, using a standard view file <strong>does </strong>work here.</p>
<h3>The Exception Object</h3>
<p>To obtain the exception object within the controller action you can call:</p>
<pre>@error = request.env[Rack::RouteExceptions::EXCEPTION]</pre>
<h3>Source for a Custom Exception Handler</h3>
<p><script src="http://gist.github.com/482348.js"> </script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xambr.com/2010/07/19/ramaze-error-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Ramaze with Unicorn, Nginx and God</title>
		<link>http://blog.xambr.com/2010/01/16/running-ramaze-with-unicorn-nginx-and-god/</link>
		<comments>http://blog.xambr.com/2010/01/16/running-ramaze-with-unicorn-nginx-and-god/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 01:19:15 +0000</pubDate>
		<dc:creator>max.rb</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[god]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[ramaze]]></category>
		<category><![CDATA[unicorn]]></category>

		<guid isPermaLink="false">http://blog.xambr.com/?p=205</guid>
		<description><![CDATA[I&#8217;ve been meaning to write a post about running a Ramaze website under a reasonable setup for a long time now so I&#8217;m happy to finally get to it. I&#8217;m going to focus my attention on the configuration of the setup vs the installation of the software - RACK, Ramaze, Unicorn and God are ruby gems and there are plenty of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been meaning to write a post about running a <a href="http://ramaze.net/" target="_blank">Ramaze</a> website under a reasonable setup for a long time now so I&#8217;m happy to finally get to it. I&#8217;m going to focus my attention on the configuration of the setup vs the installation of the software - <a href="http://rack.rubyforge.org/" target="_blank">RACK</a>, <a href="http://ramaze.net/" target="_blank">Ramaze</a>, <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> and <a href="http://god.rubyforge.org/" target="_blank">God</a> are ruby gems and there are plenty of good articles about installing <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> out there already.</p>
<h2>What You Need</h2>
<p>I&#8217;ve included the version number of the software I&#8217;ve tested this setup with but I suspect this configuration will work with newer versions as well (and probably older versions too).</p>
<ol>
<li><a href="http://rack.rubyforge.org/" target="_blank">RACK</a> (web server interface)
<ul>
<li>1.0.1</li>
</ul>
</li>
<li><a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> (http server)
<ul>
<li>0.95.2</li>
</ul>
</li>
<li><a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> (front end reverse proxy server)
<ul>
<li>0.6.36</li>
</ul>
</li>
<li><a href="http://god.rubyforge.org/" target="_blank">God</a> (process monitoring)
<ul>
<li>0.7.13</li>
</ul>
</li>
<li><a href="http://ramaze.net/" target="_blank">Ramaze</a> (web framework)
<ul>
<li>2009-05</li>
</ul>
</li>
</ol>
<h2>Some Assumptions</h2>
<p>While the config files below make the following assumptions you should be able to handle any discrepancies by altering the paths or changing the run user.</p>
<ol>
<li>Nginx is configured to read configuration files from its <code>conf/sites-enabled</code> directory
<ul>
<li>Add <code>include &lt;path to nginx install&gt;/conf/sites-enabled/*;</code> to the <code>http</code> section of your primary <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> config file to enable this</li>
</ul>
</li>
<li>Your website will run under the www-data user</li>
<li>The <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a>, <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> and <a href="http://god.rubyforge.org/" target="_blank">God</a> config files exist within the <code>config</code> directory of your <a href="http://ramaze.net/" target="_blank">Ramaze</a> website</li>
<li>The <a href="http://rack.rubyforge.org/" target="_blank">RACK</a> config file (config.ru) is in the root of your website</li>
<li>You&#8217;ve created the <code>log</code> and <code>tmp/pids</code> directories underneath the root of your website</li>
</ol>
<h2>Configuring the Pieces</h2>
<h3>1. <a href="http://rack.rubyforge.org/">RACK</a></h3>
<p>This config file is actually a very simple ruby file, its main purpose in this situation is to load your website&#8217;s <code>app.rb</code> via the require statement. This in turn loads the rest of your site.<br />
<script src="http://gist.github.com/278861.js?file=config.ru"></script></p>
<h3>3. <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a></h3>
<p>While I&#8217;ve commented the major parts of the configuration file inline, I highly recommend checking out the <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn website</a> and this <a href="http://unicorn.bogomips.org/examples/unicorn.conf.rb" target="_blank">sample configuration file</a> for more details/features.  <script src="http://gist.github.com/278861.js?file=unicorn.conf"></script></p>
<h3>2. <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a></h3>
<p>You can find more info about configuring Nginx <a href="http://wiki.nginx.org/NginxConfiguration">here</a>.  If assumptions #2 &amp; #4 are true then you&#8217;ll need to create a symlink in the <code>&lt;path-to-nginx-install&gt;/conf/sites-enabled/*</code> directory to your <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> config in the config directory under the root of your website. You can do this by executing</p>
<pre class="code">ln -s /var/www/apps/my.domain.com/config/nginx.conf &lt;path-to-nginx-install&gt;/conf/sites-enabled/my.domain.com</pre>
<p><script src="http://gist.github.com/278861.js?file=nginx.conf"></script></p>
<h3>4. God</h3>
<p>As with the RACK and Unicorn config files, this one is also written in Ruby. The God home page has a much more detailed breakdown of the config file and is definitely worth a read. <script src="http://gist.github.com/278861.js?file=god.conf"></script></p>
<h2>Flicking the Switch</h2>
<p>Once you have your configuration files in place and your website on your target server you&#8217;re going to need to turn it all on. The first step is to tell <a href="http://god.rubyforge.org/" target="_blank">God</a> to keep an eye on your <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> process and the second step is to cycle <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> forcing it to pickup your new website&#8217;s config file.</p>
<h3>Alerting <a href="http://god.rubyforge.org/" target="_blank">God</a></h3>
<p>The first thing to do is start <a href="http://god.rubyforge.org/" target="_blank">God</a> and have it monitor your master <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> process for your new website.  If <a href="http://god.rubyforge.org/" target="_blank">God</a> is not already running you can start it using:</p>
<pre class="code">god --log-level=debug</pre>
<p>To have it start monitoring your <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> process, execute the following:</p>
<pre class="code">sudo god load /var/www/apps/my.domain.com/config/god.conf</pre>
<h3>Cycling <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a></h3>
<p>Cycling <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> is something that cries out for a script so here is a simple one:<br />
<script src="http://gist.github.com/278905.js?file=cycle-nginx.sh"></script></p>
<h2>Dealing with Errors</h2>
<p>After flicking the switch for the first time under this setup I&#8217;ve run into a 502 bad gateway error multiple times. Each time it was because I had forgotten to create the required <code>tmp/pids</code> or <code>log</code> directories under the website root. If you do run into a 502 its probably because <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> had trouble starting up or <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> is not pointing to the right spot.</p>
<h4>Debugging</h4>
<p>Check the <a href="http://god.rubyforge.org/" target="_blank">God</a> logs for your website by executing:</p>
<pre class="code">god --log-level debug log my.domain.com</pre>
<p>That will tell you if <a href="http://god.rubyforge.org/" target="_blank">God</a> has managed to start the <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> process successfully. If it has then checkout the <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> error logs in the log directory of your website. If it cannot start the process and doesn&#8217;t indicate why then try executing the <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> startup command in a shell directly (that should at least give you the error message Unicorn is generating).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xambr.com/2010/01/16/running-ramaze-with-unicorn-nginx-and-god/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ramaze Partials with Etanni</title>
		<link>http://blog.xambr.com/2009/11/26/ramaze-partials-with-etanni/</link>
		<comments>http://blog.xambr.com/2009/11/26/ramaze-partials-with-etanni/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 06:14:32 +0000</pubDate>
		<dc:creator>max.rb</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[etanni]]></category>
		<category><![CDATA[ramaze]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.xambr.com/?p=176</guid>
		<description><![CDATA[Innate/Ramaze Version: 2009-10 Template Engine: Etanni The title of this blog post is a little inaccurate as what I&#8217;m actually going to describe are Etanni partials but considering Etanni is the default template engine in Innate/Ramaze 2009-10 I figured &#8220;Ramaze Partials&#8221; was a fair generalization. Etanni partials are easy to work with and as Pistos [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Innate/Ramaze Version:</strong> 2009-10<br />
<strong>Template Engine:</strong> Etanni</p>
<p>The title of this blog post is a little inaccurate as what I&#8217;m actually going to describe are Etanni partials but considering Etanni is the default template engine in Innate/Ramaze 2009-10 I figured &#8220;Ramaze Partials&#8221; was a fair generalization.</p>
<p>Etanni partials are easy to work with and as Pistos points out below they are documented in the <a href="http://doc.rubyists.com/ramaze+innate/Innate/Helper/Render.html#render_partial-instance_method">rdocs</a>. That said, when I went about looking for info on them (using Google) I missed the rdocs and could only find brief mentions of a &#8220;render helper&#8221; in the blogosphere. My hope is that this post will make it easier to find this information.</p>
<h2>The Render Helper</h2>
<p>This <a href="http://github.com/manveru/innate/blob/master/lib/innate/helper/render.rb" target="_blank">render helper</a> can be found in the <a href="http://github.com/manveru/innate">Innate code base</a> &#8211; it is clearly commented and worth a look if you&#8217;ve got a few minutes. There are 3 methods that this helper provides to aid you in the creation of your partials:</p>
<ol>
<li><span style="font-family: monospace; ">render_partial</span></li>
<li><span style="font-family: monospace;">render_view</span></li>
<li><span style="font-family: monospace;">render_file</span></li>
</ol>
<p>I will only cover <code>render_partial</code> as it is the only one that I&#8217;ve needed to use thus far.</p>
<h2>render_partial</h2>
<p><code>render_partial(action_name, variables = {})</code></p>
<p>The <code>action_name</code> parameter references a controller method and the <code>variables</code> parameter is a hash which is turned into members which are locally accessible within that method. Note that <code>action_name</code> doesn&#8217;t have to actually have a method declaration within a controller &#8211; it can simply be the name of a view.</p>
<h3>Example</h3>
<p>Here is the example code for a rendering contact information via an Etanni partial:</p>
<p><script src="http://gist.github.com/243258.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xambr.com/2009/11/26/ramaze-partials-with-etanni/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
