<?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; CRUD</title>
	<atom:link href="http://mastrodonato.info/index.php/tag/crud/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>Wed, 08 Sep 2010 14:27:24 +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>Aggiungere un operazione ad una risorsa in un sistema RESTful</title>
		<link>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/</link>
		<comments>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 10:31:00 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[CRUD]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[risorsa]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=31</guid>
		<description><![CDATA[Creare un applicazione RESTful con rails è molto semplice: C:>rails MyApp In un sistema REST, l&#8217;applicazione è composta da risorse web aventi un insieme vincolato di operazioni. Con lo scaffold generiamo una risorsa e le quattro basilari funzioni per gestirla, denominate CRUD (Create, Read, Update, Delete) C:>script/generate scaffold assets name:string Abbiamo creato la base da [...]]]></description>
			<content:encoded><![CDATA[<p>Creare un applicazione <a href="http://it.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">RESTful</a> con rails è molto semplice:</p>
<p><code>C:>rails MyApp</code></p>
<p>In un sistema REST, l&#8217;applicazione è composta da risorse web aventi un insieme vincolato di operazioni. Con lo scaffold generiamo una risorsa e le quattro basilari funzioni per gestirla, denominate <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank">CRUD</a> (Create, Read, Update, Delete)</p>
<p><code>C:>script/generate scaffold assets name:string</code></p>
<p>Abbiamo creato la base da cui partire per testare questo breve articolo.</p>
<p>Supponiamo di voler aggiungere una nuova operazione alla nostra risorsa risorsa <em>Asset</em>, per esempio vogliamo gestire la copia. Iniziamo creando il nuovo indirizzamento modificando quanto creato dallo scaffold:</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;">resources</span> <span style="color:#ff3333; font-weight:bold;">:assets</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:copy</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:get</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Testiamo l&#8217;esito:<br />
<code>C:>rake routes</code></p>
<p>verificando che ci sia il nuovo percorso:<br />
<code><br />
copy_asset GET   /assets/:id/copy(.:format)   {:controller=>"assets", :action=>"copy"}<br />
</code></p>
<p>La copia è un&#8217;operazione utile in molti contesti in quanto permette di effettuare l&#8217;inserimento di una nuova risorsa partendo da una già esistente.<br />
Traduciamo in codice e andiamo a creare la nuova operazione 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/assets_controller.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># GET /assets/copy</span>
<span style="color:#008000; font-style:italic;"># GET /assets/copy.xml</span>
<span style="color:#9966CC; font-weight:bold;">def</span> copy
  <span style="color:#0066ff; font-weight:bold;">@asset</span> = asset.<span style="color:#9900CC;">new</span>
  <span style="color:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">attributes</span> = Asset.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">attributes</span>
&nbsp;
  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:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:new</span> <span style="color:#006600; font-weight:bold;">&#125;</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:#0066ff; font-weight:bold;">@asset</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>

<p>Eventualmente si può creare la view, dipende dall&#8217;operazione che si deve gestire. In questo esempio si sfrutta la view dell&#8217;operazione new in quanto sono molto simili e richiamano entrambe la :create.</p>
<p>Creare o non creare una nuova azione sulla risorsa è pura filosofia, tralasciando la questione vediamo come indirizzare una nuova create, ci riferiamo ad una collezione di risorse:</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;">resources</span> <span style="color:#ff3333; font-weight:bold;">:assets</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:copy</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:get</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:collection</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:createcp</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>ho utilizzato una collezione perchè in questo caso non serve un riferimento ad una risorsa esistente, se avessi usato :member il path per la generazione dell&#8217;uri lo avrebbe richiesto.</p>
<p>se testiamo nuovamente le routes dovrebbe aggiungersi questa nuova:<br />
<code><br />
createcp_asset PUT   /assets/createcp(.:format)   {:controller=>"assets", :action=>"createcp"}<br />
</code></p>
<p>creiamo quindi la nuova view e il form con la uri della nuova azione specificando il metodo (per precisione ho indicato put ma equivale ad utilizzare un post) :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#app/views/assets/copy.html.erb
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@asset, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> createcp_assets_path, <span style="color:#ff3333; font-weight:bold;">:method</span><span style="color:#006600; font-weight:bold;">=&gt;</span>:put <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:create</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>e la nuova azione 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/assets_controller.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># POST /assets/createcp</span>
<span style="color:#008000; font-style:italic;"># POST /assets/createcp.xml</span>
<span style="color:#9966CC; font-weight:bold;">def</span> createcp
  <span style="color:#0066ff; font-weight:bold;">@asset</span> = Asset.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:asset</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#do something</span>
&nbsp;
  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:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">save</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</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;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:model</span> <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:#996600;">'model.asset'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> redirect_to assets_path <span style="color:#006600; font-weight:bold;">&#125;</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:#0066ff; font-weight:bold;">@asset</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;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:location</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@asset</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;copy&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</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:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">errors</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Per richiamare la nuova operazione dalla index:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#app/views/assets/index.html.erb
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:copy</span><span style="color:#006600; font-weight:bold;">&#41;</span>, copy_asset_path<span style="color:#006600; font-weight:bold;">&#40;</span>asset<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Infine ed eventualmente, queste sono le parole utilizzate dall&#8217;esempio per l&#8217;internazionalizzazione:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">create: <span style="color:#996600;">&quot;Crea&quot;</span>
copy: <span style="color:#996600;">&quot;Copia&quot;</span>
created: <span style="color:#996600;">&quot;L'oggetto {{model}} è stato creato correttamente.&quot;</span>
name: <span style="color:#996600;">&quot;Nome&quot;</span>
model:
  asset: <span style="color:#996600;">&quot;Allegato&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
