<?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>Rails on the Run &#187; xml</title>
	<atom:link href="http://railsontherun.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://railsontherun.com</link>
	<description>Rails experiments by Matt Aimonetti</description>
	<lastBuildDate>Tue, 23 Feb 2010 07:28:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Speed up your Rails XML responses</title>
		<link>http://railsontherun.com/2010/02/22/speed-up-your-rails-xml-responses/</link>
		<comments>http://railsontherun.com/2010/02/22/speed-up-your-rails-xml-responses/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 07:25:09 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[benchmarks]]></category>
		<category><![CDATA[nokogiri]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://railsontherun.com/?p=1734</guid>
		<description><![CDATA[At work, we have an XML API that gets quite a lot of traffic. Last week I looked into improving its performance since we are expecting more traffic soon and want to make sure our response time is optimized. My first thought was to make sure we had  an optimized ActiveSupport&#8217;s xmlmini backend. Rails 2.3.5 [...]]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://www.us.playstation.com/">work</a>, we have an XML API that gets quite a lot of traffic. Last week I looked into improving its performance since we are expecting more traffic soon and want to make sure our response time is optimized.</p>
<p>My first thought was to make sure we had  an optimized <a href="http://api.rubyonrails.org/classes/ActiveSupport/XmlMini.html" target="_blank">ActiveSupport&#8217;s xmlmini backend</a>. Rails 2.3.5 fixed some issues when using <a title="Nokogiri" href="http://nokogiri.org/" target="_blank">Nokogiri</a> as a xmlmini so I switched to my favorite Ruby XML lib:</p>
<pre class="brush:ruby">ActiveSupport::XmlMini.backend = 'Nokogiri'</pre>
<p>I run some benchmarks using ab, httperf and jmeter but the results were not that great. I was so sure that switching from <a href="http://ruby-doc.org/stdlib/libdoc/rexml/rdoc/index.html" target="_blank">rexml</a> to <a href="http://nokogiri.org/" target="_blank">nokogiri</a> would give me awesome results that I was very disappointed.</p>
<p>I was about to call <a href="http://tenderlovemaking.com/" target="_blank">Aaron Patterson</a> (Nokogiri&#8217;s author) to insult him, blame him for _why&#8217;s disappearance and tell him that all his pro bono efforts were useless since my own app was not running much faster when switched to his library. As I was about to dial his number on my iPhone I had a crazy thought&#8230; maybe it was not Aaron&#8217;s fault, maybe it was mine.</p>
<p>So I took a break went to play some fuzzball and as I was being ridiculed by Italian coworker, Emanuele, I realized that most of our API calls were just simple HTTP requests with no XML payload, just some query params. However, we were generating a lot of XML to send back to the client and AS::XmlMini only takes care of the XML parsing, not the rendering.</p>
<p>The XML rendering is done by <a href="http://onestepback.org/" target="_blank">Jim Weirich</a>&#8216;s pure Ruby <a href="http://builder.rubyforge.org/" target="_blank">builder library</a> which is vendored in Rails. Builder does a good job, but I thought that maybe a C based library might improve the speed. A coworker of mine (James Bunch) recommended to look into <a href="http://github.com/codahale/faster-builder" target="_blank">faster-builder</a>, a drop-in Builder replacement based on libxml. Unfortunately, the project doesn&#8217;t seem to be maintained and I decided to look into using <a href="http://nokogiri.org/">Nokogiri</a> XML builder instead. (Also, faster-builder&#8217;s author doesn&#8217;t like me very much while Aaron knows he&#8217;s one of my Ruby heroes so asking for help could be easier)</p>
<p>Some people reported having tried using <a href="http://nokogiri.org/">Nokogiri</a> as a XML builder but didn&#8217;t see much speed improvement. Because of the amount of magic required to render a rxml template, I was not really surprised but I decided to contact Aaron and ask him if he tried using his lib instead of builder in a Rails app. <a href="http://www.flickr.com/photos/aaronp/57241193/" target="_blank">Aaron</a> told me he gave it a try a while back and he helped me get my Rails app setup to render xml templates using <a href="http://nokogiri.org/">Nokogiri</a>.</p>
<p>The next step was simple, create a <a href="http://github.com/mattetti/noko-vs-builder-benchmark" target="_blank">benchmark app</a> and benchmark Builder vs Nokogiri using various templates. Here are the results I got using Ruby 1.9.1 (the Ruby version we use in production) and two sets of templates:</p>
<p><strong>Builder</strong> small template, <strong>time per request: 15.507 [ms]</strong> (mean)</p>
<pre class="brush:bash/shell">$ ab -c 1 -n 200 http://127.0.0.1:3000/benchmarks/builder_small
This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests

Server Software:        nginx/0.7.65
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /benchmarks/builder_small
Document Length:        216 bytes

Concurrency Level:      1
Time taken for tests:   3.101 seconds
Complete requests:      200
Failed requests:        0
Write errors:           0
Total transferred:      114326 bytes
HTML transferred:       43200 bytes
Requests per second:    64.49 [#/sec] (mean)
Time per request:       15.507 [ms] (mean)
Time per request:       15.507 [ms] (mean, across all concurrent requests)
Transfer rate:          36.00 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:    11   15   8.8     12      47
Waiting:        3   15   8.9     12      47
Total:         11   15   8.8     12      47

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     12
  75%     13
  80%     13
  90%     35
  95%     36
  98%     38
  99%     41
 100%     47 (longest request)</pre>
<p><strong>Nokogiri</strong> small template, <strong>time per request: 15.354 [ms] (mean)</strong></p>
<pre class="brush:bash">$ ab -c 1 -n 200 http://127.0.0.1:3000/benchmarks/noko_small
This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests

Server Software:        nginx/0.7.65
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /benchmarks/noko_small
Document Length:        238 bytes

Concurrency Level:      1
Time taken for tests:   3.071 seconds
Complete requests:      200
Failed requests:        0
Write errors:           0
Total transferred:      118717 bytes
HTML transferred:       47600 bytes
Requests per second:    65.13 [#/sec] (mean)
Time per request:       15.354 [ms] (mean)
Time per request:       15.354 [ms] (mean, across all concurrent requests)
Transfer rate:          37.75 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:    11   15   8.6     12      39
Waiting:       11   15   8.6     12      39
Total:         11   15   8.6     12      39

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     12
  75%     12
  80%     13
  90%     35
  95%     36
  98%     37
  99%     38
 100%     39 (longest request)</pre>
<p>Running the benchmarks many times showed that Nokogiri and Builder were taking more or less the same amount of time to builder a small template.</p>
<p>I then decided to try a bigger template, closer to what we have in production, here are the results:</p>
<p><strong>Nokogiri</strong> longer template, <strong>time per request: 31.252 [ms] (mean)</strong></p>
<pre class="brush:bash">$ ab -c 1 -n 200 http://127.0.0.1:3000/benchmarks/noko
This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests

Server Software:        nginx/0.7.65
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /benchmarks/noko
Document Length:        54398 bytes

Concurrency Level:      1
Time taken for tests:   6.250 seconds
Complete requests:      200
Failed requests:        0
Write errors:           0
Total transferred:      10951200 bytes
HTML transferred:       10879600 bytes
Requests per second:    32.00 [#/sec] (mean)
Time per request:       31.252 [ms] (mean)
Time per request:       31.252 [ms] (mean, across all concurrent requests)
Transfer rate:          1711.00 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:    24   31  11.3     26      62
Waiting:       23   30  11.3     24      61
Total:         24   31  11.3     26      62

Percentage of the requests served within a certain time (ms)
  50%     26
  66%     27
  75%     27
  80%     29
  90%     54
  95%     55
  98%     58
  99%     59
 100%     62 (longest request)</pre>
<p><strong>Builder</strong>, longer template, <strong>Time per request: 140.725 [ms] (mean)</strong></p>
<pre class="brush:bash">$ ab -c 1 -n 200 http://127.0.0.1:3000/benchmarks/builder
This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests

Server Software:        nginx/0.7.65
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /benchmarks/builder
Document Length:        54376 bytes

Concurrency Level:      1
Time taken for tests:   28.145 seconds
Complete requests:      200
Failed requests:        0
Write errors:           0
Total transferred:      10947000 bytes
HTML transferred:       10875200 bytes
Requests per second:    7.11 [#/sec] (mean)
Time per request:       140.725 [ms] (mean)
Time per request:       140.725 [ms] (mean, across all concurrent requests)
Transfer rate:          379.83 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:   127  141  24.6    132     331
Waiting:      126  139  23.6    130     328
Total:        127  141  24.6    132     331

Percentage of the requests served within a certain time (ms)
  50%    132
  66%    138
  75%    147
  80%    149
  90%    156
  95%    169
  98%    193
  99%    311
 100%    331 (longest request)</pre>
<p>Wow, <a title="Aaron Patterson" href="http://twitter.com/tenderlove" target="_blank">@tenderlove</a>&#8216;s Nokogori just brought us a<strong> 4.5X speed improvement for this specific template</strong>.  100ms per request is probably not a big deal for most people and I have to say that Jim did a great job with Builder. However in my specific case, 100ms on a request being called thousands of times per hour is quite important.</p>
<p>(The <a href="http://github.com/mattetti/noko-vs-builder-benchmark" target="_blank">benchmark app is available on github</a>, feel free to fork it and benchmark your own templates)</p>
<p>Who would have thought that a man like this could save the day?!</p>
<div id="attachment_1737" class="wp-caption alignleft" style="width: 160px"><a href="http://railsontherun.com/wp-content/uploads/2010/02/aaron.jpg"><img class="size-thumbnail wp-image-1737 " title="Aaron 'Tenderlove' Patterson" src="http://railsontherun.com/wp-content/uploads/2010/02/aaron-150x150.jpg" alt="" width="150" height="150" /></a><p class="wp-caption-text">Aaron &#39;Tenderlove&#39; Patterson</p></div>
<p><a href="http://www.flickr.com/photos/aaronp/3824959062/"><img class="alignright" title="Aaron" src="http://farm3.static.flickr.com/2470/3824959062_fb0755e665_m_d.jpg" alt="" width="180" height="240" /></a><br />
<br style="clear: both;" /><br />
<a href="http://www.flickr.com/photos/aaronp/57241193/"><img class="alignleft" title="Aaron Patterson" src="http://farm1.static.flickr.com/29/57241193_8137f2a4af_m_d.jpg" alt="" width="240" height="180" /></a></p>
<p><a href="http://www.flickr.com/photos/aaronp/3132124227/"><img class="alignright" title="Aaron Patterson" src="http://farm4.static.flickr.com/3289/3132124227_3ace4ec7ae_m_d.jpg" alt="" width="180" height="240" /></a><br style="clear: both;" /></p>
<p><strong><em>The moral of the story is that adding a bit of tenderlove to your Ruby code can make it perform much much better!</em></strong></p>
<p style="text-align: center;"><span style="text-decoration: underline;"><strong>Thank you Aaron &#8216;Tenderlove&#8217; Patterson!</strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2010/02/22/speed-up-your-rails-xml-responses/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Sexy charts in less than 5 minutes</title>
		<link>http://railsontherun.com/2007/10/04/sexy-charts-in-less-than-5-minutes/</link>
		<comments>http://railsontherun.com/2007/10/04/sexy-charts-in-less-than-5-minutes/#comments</comments>
		<pubDate>Thu, 04 Oct 2007 03:06:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[5 minutes]]></category>
		<category><![CDATA[amcharts]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[gruff]]></category>
		<category><![CDATA[jfreechart]]></category>
		<category><![CDATA[rails 2.0]]></category>
		<category><![CDATA[swfobject]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml builder]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/11/05/sexy-charts-in-less-than-5-minutes</guid>
		<description><![CDATA[NOV 04 Update: demo app now available there. Sexy charts BDD style presentation at the SDRuby group to be posted soon on video podcast Last time, in our &#8216;do it in less than 5 minutes&#8217; series, we saw how to add quickly and simply add Ajax pagination. This time we&#8217;ll see how to add some [...]]]></description>
			<content:encoded><![CDATA[<h2>NOV 04 Update: demo app now available <a href="http://railsontherun.com/assets/sexy_charts.zip">there</a>. Sexy charts BDD style presentation at the <a href="http://sdruby.com">SDRuby group</a> to be posted soon on <a href="http://podcast.sdruby.com/">video podcast</a></h2>
<p><a href="http://railsontherun.com/2007/9/27/ajax-pagination-in-less-than-5-minutes">Last time</a>, in our <em>&#8216;do it in less than 5 minutes&#8217;</em> series, we saw how to add <a href="http://railsontherun.com/2007/9/27/ajax-pagination-in-less-than-5-minutes">quickly and simply add Ajax pagination</a>.</p>
<p>This time we&#8217;ll see how to add some sexy/fancy charts to your rails app.</p>
<p>The goal is to end up with something like:</p>
<p><img src="http://farm2.static.flickr.com/1322/1480241002_e67637a659_o.png" alt="chart"/></p>
<p><img src="http://farm2.static.flickr.com/1414/1479377969_805d23a55d_o.png" alt="charts2"/></p>
<h2>Various options</h2>
<p>You might have heard or even tried solution such as <a href="http://nubyonrails.com/pages/gruff">Gruff</a> or <a href="http://www.jfree.org/jfreechart/">JFreeChart</a>.</p>
<p>While these solutions are great, they are certainly a pain in the butt. Gruff requires RMagick (avoid RMagick as much as can) and creates static files (a real pain when your graphs change all the time) JFreeChart on the other hand requires Java, Java skills and I hate <a href="http://wiki.rubyonrails.org/rails/pages/HowtoGenerateJFreeCharts">the way</a> you create graphs:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  <span class="r">def</span> <span class="fu">CreateChart</span><tt>
</tt>         pipe = <span class="co">IO</span>.popen <span class="s"><span class="dl">&quot;</span><span class="k">java -cp C:</span><span class="ch">\\</span><span class="k">InstantRails</span><span class="ch">\\</span><span class="k">rails_apps</span><span class="ch">\\</span><span class="k">project</span><span class="ch">\\</span><span class="k">jfree</span><span class="ch">\\</span><span class="k">src;C:</span><span class="ch">\\</span><span class="k">InstantRails</span><span class="ch">\\</span><span class="k">rails_apps</span><span class="ch">\\</span><span class="k">project</span><span class="ch">\\</span><span class="k">jfree</span><span class="ch">\\</span><span class="k">lib</span><span class="ch">\\</span><span class="k">jcommon-1.0.0-rc1.jar;C:</span><span class="ch">\\</span><span class="k">InstantRails</span><span class="ch">\\</span><span class="k">rails_apps</span><span class="ch">\\</span><span class="k">project</span><span class="ch">\\</span><span class="k">jfree</span><span class="ch">\\</span><span class="k">lib</span><span class="ch">\\</span><span class="k">jfreechart-1.0.0-rc1.jar; CreateChart</span><span class="dl">&quot;</span></span> <tt>
</tt>         pipe.close<tt>
</tt>         redirect_to <span class="s"><span class="dl">&quot;</span><span class="k">/graph/report</span><span class="dl">&quot;</span></span> <tt>
</tt>      <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Anyway, none of these solutions would let us create our charts in less than 5 minutes so let&#8217;s cut the story short. The best solution IMHO is to use Flash. But <em>wait</em>, you don&#8217;t need to know ActionScript or to own a license of Flash or Flex, we have libraries available for us to use without any Flash knowledge <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.maani.us/xml_charts/index.php?menu=Gallery">XML/SWF</a> is cool Flash library which should fulfill our needs, you can even find a <a href="http://ziya.liquidrail.com/">rails plugin</a> to make things easier.</p>
<h2>amCharts</h2>
<p>But, to be honest I&#8217;d like to have something a bit &#8220;cleaner/sexy/fancy&#8221; and easier to setup.  So we&#8217;re going to use <a href="http://www.amcharts.com/">amCharts</a>  Don&#8217;t get me wrong, XML/SWF is a great library and you can make your graphs look nice (but you have to pay for support).<br />
Since we are running out of time let&#8217;s see how to implement a nice graph using *my* favorite library.</p>
<p><img src="http://www.amcharts.com/images/logo.gif" alt="amcharts"/></p>
<p>[DISCLAIMER: amCharts is <em>NOT open source</em> and <em>NOT free</em>. But, it's <em>cheap</em> (85 euros per site) especially when you think of how much time you will save. AND there is a <em>FREE version</em>. The Free version is the same as the full version but with a link back to amcharts.com]</p>
<h2>Setup</h2>
<p>Let&#8217;s go ahead and download one of the package: <a href="http://www.amcharts.com/column/download/">http://www.amcharts.com/column/download/</a> for instance.</p>
<p>Unpack the files and put them in their own folder in your public folder.<br />
Make sure you have the .swf file (amcolumn.swf for instance), a XML settings file and the fonts folder.<br />
(You might want to also create an empty amcharts_key.txt in the same folder since the plugin tries to load the key and you don&#8217;t want to pollute your logs.)</p>
<h2>Usage</h2>
<p>Now you need to understand how amCharts works. </p>
<p>After being loaded, amCharts expects a datastream. The datastream is then parsed and displayed as a chart.<br />
You can modify the aspect of any chart by changing its settings.<br />
Settings are set at runtime and/or in a setting file.</p>
<p>Great! I won&#8217;t cover the settings file. It&#8217;s a well documented XML file you just copied in your public folder. (or check the documentation)</p>
<p>What we want to focus on, is the <em>datastream</em>. Basically we just need to create a XML file that can be parsed by amCharts.</p>
<p>Let&#8217;s imagine that we have a reports_controller.rb file  We want to display the population of the cities in California.</p>
<p>let&#8217;s add a new action to render our XML file:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  <span class="r">def</span> <span class="fu">population</span><tt>
</tt>    <span class="iv">@cities</span> = <span class="co">City</span>.find(<span class="sy">:all</span>)<tt>
</tt>    <span class="iv">@population_data_link</span> = formatted_population_reports_url(<span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml</span>)<tt>
</tt>    respond_to <span class="r">do</span> |format|<tt>
</tt>      format.html<tt>
</tt>      format.xml  { render <span class="sy">:action</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">population.xml.builder</span><span class="dl">&quot;</span></span>, <span class="sy">:layout</span> =&gt; <span class="pc">false</span> }<tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>(notice that I&#8217;m using rails 2.0 and that&#8217;s why my XML template is not RXML)</p>
<p>As you can see, we have 2 values: @cities and @population<em>data</em>link</p>
<p>@cities contains all the City records, including their population etc..</p>
<p>@population<em>data</em>link contains the url to retrieve the datastream.</p>
<p>If you wonder how I got this url? I&#8217;m simply using a named route defined in my routes.rb:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  map.resources <span class="sy">:reports</span>, <span class="sy">:collection</span> =&gt; {<span class="sy">:population</span> =&gt; <span class="sy">:get</span>}</pre>
</td>
</tr>
</table>
<p>(note that you don&#8217;t need to create a restful route for that, a simple named route would have worked too)</p>
<h2>Flash detection</h2>
<p>Since we are going to use Flash, we want to make sure that people have the Flash plugin installed on their browser. For that we will use <a href="http://blog.deconcept.com/swfobject/">swfobject</a>. Simply make sure to add swfobject.js (available in any amChart package) to your public/javascript folder. Then make sure you linked the javascript in your header:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  &lt;%= javascript_include_tag <span class="s"><span class="dl">'</span><span class="k">swfobject</span><span class="dl">'</span></span> <span class="s"><span class="dl">%&gt;</span></span></pre>
</td>
</tr>
</table>
<p>We now need to create our 2 views: <em>population.html.erb</em> and <em>population.xml.builder</em></p>
<h2>population.html.erb</h2>
<p>Basically, this view only loads amCharts and provides it with the details of the datastream:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt><strong>15</strong><tt>
</tt>16<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  &lt;div id=<span class="s"><span class="dl">&quot;</span><span class="k">population_chart</span><span class="dl">&quot;</span></span> <span class="r">class</span>=<span class="s"><span class="dl">'</span><span class="k">chart</span><span class="dl">'</span></span>&gt;<tt>
</tt>    &lt;strong&gt;<span class="co">Text</span> displayed <span class="r">when</span> the user doesn<span class="s"><span class="dl">'</span><span class="k">t have Flash. You might want to display a simple table with the population, search engines and visitor without flash would love that.&lt;/strong&gt;<tt>
</tt>    &lt;p&gt; To see this page properly, you need to upgrade your Flash Player, please visit the Adobe web site&lt;/p&gt;<tt>
</tt>  &lt;/div&gt;<tt>
</tt><tt>
</tt>  &lt;script type=&quot;text/javascript&quot;&gt;<tt>
</tt>    // &lt;![CDATA[    <tt>
</tt>    var so = new SWFObject(&quot;/amcolumn/amcolumn.swf&quot;, &quot;population_chart&quot;, &quot;800&quot;, &quot;380&quot;, &quot;8&quot;, &quot;#000000&quot;);<tt>
</tt>    so.addVariable(&quot;path&quot;, &quot;/amcolumn/&quot;);<tt>
</tt>    so.addVariable(&quot;settings_file&quot;, escape(&quot;/amcolumn/column_settings.xml&quot;));<tt>
</tt>    so.addVariable(&quot;data_file&quot;, escape(&quot;&lt;%= @population_data_link %&gt;&quot;));<tt>
</tt>    so.addVariable(&quot;additional_chart_settings&quot;, &quot;&lt;settings&gt;&lt;labels&gt;&lt;label&gt;&lt;x&gt;250&lt;/x&gt;&lt;y&gt;25&lt;/y&gt;&lt;text_size&gt;18&lt;/text_size&gt;&lt;text&gt;&lt;![CDATA[&lt;b&gt;California Population as of &lt;%= Time.now.to_s(:db) %&gt;&lt;/b&gt;]]&gt;&lt;/text&gt;&lt;/label&gt;&lt;/labels&gt;&lt;/settings&gt;&quot;);<tt>
</tt>    so.addVariable(&quot;preloader_color&quot;, &quot;#000000&quot;);<tt>
</tt>    so.write(&quot;population_chart&quot;);<tt>
</tt>    // ]]&gt;<tt>
</tt>  &lt;/script&gt;</span></span></pre>
</td>
</tr>
</table>
<p>As you can see, we have a div called population_chart. This div is replaced at load time by the Flash object if the visitor has Flash setup locally. Think about providing some data in case the user doesn&#8217;t have Flash.</p>
<p>The rest is simple Javascript. I unpacked the amchart column lib in mypublic/amcolumn folder and that&#8217;s why I setup the path as &#8220;amcolumn&#8221;</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  so.addVariable(<span class="s"><span class="dl">&quot;</span><span class="k">path</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">/amcolumn/</span><span class="dl">&quot;</span></span>);</pre>
</td>
</tr>
</table>
<p>My settings file is called column_settings.xml :</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  so.addVariable(<span class="s"><span class="dl">&quot;</span><span class="k">settings_file</span><span class="dl">&quot;</span></span>, escape(<span class="s"><span class="dl">&quot;</span><span class="k">/amcolumn/column_settings.xml</span><span class="dl">&quot;</span></span>));</pre>
</td>
</tr>
</table>
<p>and the most important part:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  so.addVariable(<span class="s"><span class="dl">&quot;</span><span class="k">data_file</span><span class="dl">&quot;</span></span>, escape(<span class="s"><span class="dl">&quot;</span><span class="k">&lt;%= @population_data_link %&gt;</span><span class="dl">&quot;</span></span>));</pre>
</td>
</tr>
</table>
<p>Finally, I added some dynamic settings just to show you how easy it is:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  so.addVariable(<span class="s"><span class="dl">&quot;</span><span class="k">additional_chart_settings</span><span class="dl">&quot;</span></span>,<tt>
</tt>  <span class="s"><span class="dl">&quot;</span><span class="k">&lt;settings&gt;&lt;labels&gt;&lt;label&gt;&lt;x&gt;250&lt;/x&gt;&lt;y&gt;25&lt;/y&gt;&lt;text_size&gt;18&lt;/text_size&gt;&lt;text&gt;&lt;![CDATA[&lt;b&gt;California Population as of &lt;%= Time.now.to_s(:db) %&gt;&lt;/b&gt;]]&gt;&lt;/text&gt;&lt;/label&gt;&lt;/labels&gt;&lt;/settings&gt;</span><span class="dl">&quot;</span></span>);</pre>
</td>
</tr>
</table>
<p>Ok, let&#8217;s now create our XML view:</p>
<h2>population.xml.builder</h2>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt><strong>15</strong><tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt><strong>25</strong><tt>
</tt>26<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  xml.instruct! <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml</span>, <span class="sy">:version</span>=&gt;<span class="s"><span class="dl">&quot;</span><span class="k">1.0</span><span class="dl">&quot;</span></span>, <span class="sy">:encoding</span>=&gt;<span class="s"><span class="dl">&quot;</span><span class="k">UTF-8</span><span class="dl">&quot;</span></span><tt>
</tt>  xml.chart <span class="r">do</span><tt>
</tt>    <span class="c"># xml.message &quot;You can broadcast any message to chart from data XML file&quot;, :bg_color =&gt; &quot;#FFFFFF&quot;, :text_color =&gt; &quot;#000000&quot;</span><tt>
</tt>    xml.series <span class="r">do</span>    <tt>
</tt>      <span class="iv">@cities</span>.each_with_index <span class="r">do</span> |city, index|<tt>
</tt>        xml.value city.name, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index<tt>
</tt>      <span class="r">end</span><tt>
</tt>    <span class="r">end</span><tt>
</tt><tt>
</tt>    xml.graphs <span class="r">do</span><tt>
</tt>     <span class="c">#the gid is used in the settings file to set different settings just for this graph</span><tt>
</tt>      xml.graph <span class="sy">:gid</span> =&gt; <span class="s"><span class="dl">'</span><span class="k">population</span><span class="dl">'</span></span> <span class="r">do</span><tt>
</tt>        <span class="iv">@cities</span>.each_with_index <span class="r">do</span> |city, index|<tt>
</tt>          population = city.population<tt>
</tt>          <span class="r">case</span> population<tt>
</tt>            <span class="c"># When the population is &gt; 1 million, show the bar in red/pink</span><tt>
</tt>            <span class="r">when</span> &gt; <span class="i">100000</span><tt>
</tt>              xml.value value, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index, <span class="sy">:color</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#ff43a8</span><span class="dl">&quot;</span></span>, <span class="sy">:gradient_fill_colors</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#960040,#ff43a8</span><span class="dl">&quot;</span></span>, <span class="sy">:description</span> =&gt; level<tt>
</tt>            <span class="r">else</span><tt>
</tt>              xml.value value, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index, <span class="sy">:color</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#00C3C6</span><span class="dl">&quot;</span></span>, <span class="sy">:gradient_fill_colors</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#009c9d,#00C3C6</span><span class="dl">&quot;</span></span>, <span class="sy">:description</span> =&gt; level<tt>
</tt>            <span class="r">end</span><tt>
</tt>        <span class="r">end</span><tt>
</tt>      <span class="r">end</span><tt>
</tt>    <span class="r">end</span><tt>
</tt><tt>
</tt>  <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Nothing fancy, we first created a series with all the city names:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  xml.series <span class="r">do</span>    <tt>
</tt>    <span class="iv">@cities</span>.each_with_index <span class="r">do</span> |city, index|<tt>
</tt>      xml.value city.name, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index<tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Then we created another node with the values for each city.<br />
Since it would be cool to display some bars in a different color, we used a case-switch statement:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">    xml.graph <span class="sy">:gid</span> =&gt; <span class="s"><span class="dl">'</span><span class="k">population</span><span class="dl">'</span></span> <span class="r">do</span><tt>
</tt>      <span class="iv">@cities</span>.each_with_index <span class="r">do</span> |city, index|<tt>
</tt>        population = city.population<tt>
</tt>        <span class="r">case</span> population<tt>
</tt>          <span class="c"># When the population is &gt; 1 million, show the bar in red/pink</span><tt>
</tt>          <span class="r">when</span> &gt; <span class="i">100000</span><tt>
</tt>            xml.value value, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index, <span class="sy">:color</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#ff43a8</span><span class="dl">&quot;</span></span>, <span class="sy">:gradient_fill_colors</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#960040,#ff43a8</span><span class="dl">&quot;</span></span>, <span class="sy">:description</span> =&gt; level<tt>
</tt>          <span class="r">else</span><tt>
</tt>            xml.value value, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index, <span class="sy">:color</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#00C3C6</span><span class="dl">&quot;</span></span>, <span class="sy">:gradient_fill_colors</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#009c9d,#00C3C6</span><span class="dl">&quot;</span></span>, <span class="sy">:description</span> =&gt; level<tt>
</tt>          <span class="r">end</span><tt>
</tt>      <span class="r">end</span><tt>
</tt>    <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Depending on what you want to display, you might need to have different colors or a different tooltip text, or load an animation or image&#8230;  and as you can see, it&#8217;s <em>REALLY</em> easy.</p>
<p>Got to http://yoursite.com/reports/population to enjoy your new fancy graph.</p>
<h2>That&#8217;s it, you are done! </h2>
<p>Time to tweak your settings file to make your graph look <em>awesome</em>.<br />
Since you now have a lot of free time, you can start re-factoring your code and make sure you have a good test coverage.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/10/04/sexy-charts-in-less-than-5-minutes/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>attachment_fu, Flash and to_xml tips</title>
		<link>http://railsontherun.com/2007/07/21/attachment_fu-flash-and-to_xml-tips/</link>
		<comments>http://railsontherun.com/2007/07/21/attachment_fu-flash-and-to_xml-tips/#comments</comments>
		<pubDate>Sat, 21 Jul 2007 03:30:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[attachment]]></category>
		<category><![CDATA[attachment_fu]]></category>
		<category><![CDATA[edge]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[to_xml]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/07/21/attachment_fu-flash-and-to_xml-tips</guid>
		<description><![CDATA[I recently had to deal with an interesting challenge. I had to write a simple interface between a rails app and a Flash application. Nothing hard and if you browse the archives, you&#8217;ll find examples and tutorials on how to create a REST interface to communicate between Rails and Flash. The thing was that this [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to deal with an interesting challenge. I had to write a simple interface between a rails app and a Flash application. Nothing hard and if you browse the archives, you&#8217;ll find examples and tutorials on how to create a REST interface to communicate between Rails and Flash.</p>
<p>The thing was that this time I had to interface with a model using <a href="http://svn.techno-weenie.net/projects/plugins/attachment_fu/">attachment<em>fu</a>. I&#8217;m a great fan of a</em>fu and it&#8217;s definitely the best way of dealing with uploads. </p>
<p>My model looked more or less like that:</p>
<pre><code>class Photo &lt; ActiveRecord::Base

belongs_to :user

  has_attachment(
    :content_type =&gt; :image,
    :storage =&gt; :file_system,
    :max_size =&gt; 10.megabytes,
    :resize_to =&gt; '640x480&gt;',
    :thumbnails =&gt; { :thumb =&gt; '100x100&gt;',
                              :preview =&gt; '300x200&gt;',
                     }
  )
  validates_as_attachment
  # read more about validates_existence_of http://blog.hasmanythrough.com/2007/7/14/validate-your-existence
  validates_existence_of :user
end
</code></pre>
<p>My show action in my photo controller could have looked a bit like that:</p>
<pre><code>respond_to do |format|
  format.html # show.html.erb
  format.xml  { render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @photo }
end
</code></pre>
<p>That&#8217;s great, the problem is that we are displaying a lot of information that our Flash client doesn&#8217;t need to see, actually we are exposing a lot of information nobody should ever see and we are not displaying what we should. On top of being a waste of bandwidth and giving too much work to the client, we are not actually providing the user with the details of the thumbnail.</p>
<p>The first thing to do would be not to display some of the object attributes, the to_xml method lets you do that. </p>
<p>Note that in Edge, Rails will automatically try to convert your object using to<em>xml, you don&#8217;t even need to mention it. However in our case, we want to use some _advanced</em> features offered by to_xml, and here is how our code should look like:  </p>
<pre><code>format.xml do
  render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @photo.to_xml( :except =&gt; [ :id, :filename, :updated_at, :user_id, :height, :width], :skip_types =&gt; true )
end
</code></pre>
<p>What I just did is very simple, we rendered our object as an xml object but we didn&#8217;t convert few attributes, :id, :filename, :updated<em>at, :user</em>id, :height, :width. By default Rails also adds the object type, we don&#8217;t really need that right now, so let&#8217;s skip them.<br/><br />
(The reason why I don&#8217;t want to convert the filename is that I want to provide our Flash client with the photo thumbnail instead of the original picture.)</p>
<p>As far as I know, to_xml doesn&#8217;t let you create new attributes. (if I have some time, I&#8217;ll submit a patch to get that added).</p>
<p>What we are trying to do is to display the avatar of a user. We found the photo record using @user.photo but that&#8217;s the original photo and we want to provide Flash with the avatar info, not the original.</p>
<p>What we need to do is to simply add a new attribute called avatar:</p>
<pre><code>format.xml do
  @photo[:avatar] = @photo.public_filename(:thumb)
  render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @photo.to_xml( :except =&gt; [ :id, :filename, :updated_at, :user_id, :height, :width], :skip_types =&gt; true )
end
</code></pre>
<p>Simple enough, but it took me a little while to figure it out <img src='http://railsontherun.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Voila, we now have a clean, trimmed and safe XML returned object that you can be consumed by our Flash client. Ohh, and we added a new attribute that the original object didn&#8217;t have <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/07/21/attachment_fu-flash-and-to_xml-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails/Flash, part 2</title>
		<link>http://railsontherun.com/2007/05/07/flash-rails-part2/</link>
		<comments>http://railsontherun.com/2007/05/07/flash-rails-part2/#comments</comments>
		<pubDate>Mon, 07 May 2007 22:15:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[respond_to]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/05/07/flash-rails-part2</guid>
		<description><![CDATA[In part I I explained how to access Rails data from Flash. However Yves aka Kadoudal was wondering what I did with Rails to return the event record: Rails.get(&#8216;events&#8217;, rails_events); how rails returns the event record as we don&#8217;t call a controller/action &#8230; ? I believe doing it RESTFul it&#8217;s depending upon your route ? [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.railsontherun.com/2007/4/9/rails-and-flash">part I</a> I explained how to access Rails data from Flash.</p>
<p>However Yves aka Kadoudal was wondering what I did with Rails to return the event record:</p>
<p>Rails.get(&#8216;events&#8217;, rails_events); how rails returns the event record as we don&#8217;t call a controller/action &#8230; ? I believe doing it RESTFul it&#8217;s depending upon your route ? is it not?</p>
<p>We are actually calling a controller/action If you look at the Restfulflash class, you&#8217;ll notice a function called get</p>
<pre><code>public function get(controller, callback){
        var railsReply:XML = new XML();
        railsReply.ignoreWhite = true;
        railsReply.onLoad = function(success:Boolean){
            if (success) {
                    trace ('Rails responded: '+railsReply);
                    callback.text = railsReply;
            } else {
                    trace ('Error while waiting for Rails to reply');
               callback.text = 'error';
            }
        }
        var railsRequest:XML = new XML();
        railsRequest.contentType='application/xml';
        railsRequest.sendAndLoad(this.gateway+controller, railsReply);
        delete railsRequest;
    }
</code></pre>
<p>What&#8217;s really interesting are the following 2 lines:</p>
<p>railsRequest.contentType=&#8221;application/xml&#8221;;<br />
railsRequest.sendAndLoad(this.gateway+controller, railsReply);</p>
<p>I&#8217;m preparing a request that I will send to my gateway mentioning the controller name. In the previous example I was calling the &#8216;events&#8217; controller: http://mysite.fr/events because I&#8217;m using REST and because my request is a &#8216;get&#8217; Rails will call the index action.</p>
<p>for more info on <a href="http://www.softiesonrails.com/2007/4/10/rest-101-part-3-just-call-me-the-repo-man">RESTful design</a> check the nice series available from the <a href="http://www.softiesonrails.com">softies on rails blog</a></p>
<p>Let&#8217;s just look at my code and see what&#8217;s up with rails magic <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here is my index action sending you back a different object based on the header of your request.</p>
<pre><code>class EventsController &lt; ApplicationController
  # GET /events
  # GET /events.xml
  def index
    @events = Event.find(:all)

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @events.to_xml }
      format.json { render :json =&gt; @events.to_json }
    end
  end
</code></pre>
<p>That&#8217;s it, and it was automatically created for you by the script/generate scaffold_resource command <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Since I&#8217;m answering Yves&#8217; questions, let&#8217;s look at his final question:</p>
<p>problem : what if the xml returned is not a record, but it has to be prepared by Rails&#8230;<br />
I explain, right now my rails view .. I have a javascript object passing all the info via JS<br />
I would like to replace the datasource file by a direct call to rails from Flash, so this param will disappear&#8230; so (correct me if I am wrong)<br />
    1- I need to pass a user_id value in JS to Flash in the script&#8230;<br />
    2- Flash need to send a request to Rails (controller/action/id to prepare the xml&#8230; ) to get in return a correct xlm object to be displayed</p>
<p>1- I&#8217;m not sure why you need to pass the user_id from JS to Flash but I guess Flash doesn&#8217;t know what user to query?? (anyway that would work)</p>
<p>2- If you are using REST, Flash just needs make a get call to /controller/id Make sure to set the request header type as xml, like I did in my function railsRequest.contentType=&#8221;application/xml&#8221;; Otherwise I believe (but didn&#8217;t try) that you can call /events/1.xml where 1 id the id of the event you want to retrieve.</p>
<p>By the way, it will call the show action from the events controller which looks like that:</p>
<pre><code>def show
  @event = Event.find(params[:id])

  respond_to do |format|
    format.html # show.rhtml
    format.xml  { render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @event.to_xml }
  end
end
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/05/07/flash-rails-part2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

