<?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>Marco Mastrodonato &#187; Routes</title>
	<atom:link href="http://mastrodonato.info/index.php/tag/routes/feed/" rel="self" type="application/rss+xml" />
	<link>http://mastrodonato.info</link>
	<description>Non c&#039;e&#039; prezzo per la miticita&#039;</description>
	<lastBuildDate>Mon, 06 Sep 2010 21:05:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Gestire instradamenti errati</title>
		<link>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati/</link>
		<comments>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 11:28:25 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Instradamenti]]></category>
		<category><![CDATA[Routes]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=89</guid>
		<description><![CDATA[La nostra applicazione può rispondere solo a determinate richieste, naturalmente a quelle che abbiamo previsto. Per tutte le altre viene generato un errore in base al tipo: Errori di instradamento Errori nel controller Errori di instradamento Piccola premessa: In un&#8217;applicazione tradizionale, equivaleva a richiamare una pagina inesistente ricevendo dal web server un codice 404. In [...]]]></description>
			<content:encoded><![CDATA[<p>La nostra applicazione può rispondere solo a determinate richieste, naturalmente a quelle che abbiamo previsto. Per tutte le altre viene generato un errore in base al tipo:</p>
<ul>
<li>Errori di instradamento</li>
<li>Errori nel controller</li>
</ul>
<h3>Errori di instradamento</h3>
<p><strong>Piccola premessa</strong>: In un&#8217;applicazione tradizionale, equivaleva a richiamare una pagina inesistente ricevendo dal web server un codice 404. In un framework con un pattern mvc, le richieste transitano attraverso un controller e questo ci permette di definirne il comportamento. Le richieste vengono smistate in base alle regole di instradamento ed in Rails, per convenzione, tutti i controllers possono essere indirizzati, in fondo al file routes.rb scopriamo il perchè:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config\routes.rb</span>
map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">':controller/:action/:id'</span>
map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">':controller/:action/:id.:format'</span></pre></div></div>

<p>Per fare in modo che solo determinati controllers possano essere instradati, basta eliminare quelle due righe in fondo al file che definiscono solo la forma della richiesta, lasciando solo gli instradamenti espliciti. Fine della premessa.</p>
<p>Se non esiste una rotta, viene generato un <strong>errore di instradamento</strong> (routing error) poichè Rails non sa cosa intendiamo richiamare. Per gestire questa evenienza, basta semplicemente aggiungere un&#8217;ultima strada prima di generare l&#8217;errore che percorrerà solo per informare che non ha trovato altre strade valide:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config\routes.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#aggiungiamo in ultima riga, in coda a tutto</span>
map.<span style="color:#9900CC;">catch_all</span> <span style="color:#996600;">&quot;*anything&quot;</span> , <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;home&quot;</span> , <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;unknown_request&quot;</span></pre></td></tr></table></div>

<p>Quest&#8217;ultima rotta cattura gli instradamenti non gestiti e li indirizza nel controller dove vogliamo gestirli, nell&#8217;esempio indirizza nel controller home col metodo unknown_request:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\home_controller.rb</span>
<span style="color:#9966CC; font-weight:bold;">def</span> unknown_request
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:unknown_request</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      redirect_to root_path
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span>  render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:root <span style="color:#006600; font-weight:bold;">=&gt;</span> I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:unknown_request</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Errori nel controller</h3>
<p>Se il controller è stato indirizzato non è detto che la richiesta sia comunque corretta, ognuno infatti ha una serie di operazioni che devono essere previste e devono poter operare sulla risorsa quindi gestire le eccezioni sollevate dal database.</p>
<p>Dalla versione 2.0 di rails, possiamo utilizzare rescue_from nel controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\my_controller.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MyController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  rescue_from <span style="color:#6666ff; font-weight:bold;">ActionController::UnknownAction</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:action_not_found</span>
  rescue_from <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:record_not_found</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> action_not_found
    render <span style="color:#996600;">'shared/action_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> record_not_found
    render <span style="color:#996600;">'shared/record_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Queste sono le viste:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">#app\views\sharedaction _not_found.html.erb
&lt;h2&gt;Sorry, the action doesn't exists!&lt;/h2&gt;
&nbsp;
#app\views\sharedrecord_not_found.html.erb
&lt;h2&gt;Sorry, the record doesn't exists!&lt;/h2&gt;</pre></div></div>

<p>Usare una proc come parametro a rescue_from potrebbe essere un&#8217;alternativa più concisa:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\my_controller.rb</span>
rescue_from<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> render <span style="color:#996600;">'shared/record_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
