{"id":147,"date":"2006-08-07T08:48:13","date_gmt":"2006-08-07T13:48:13","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/08\/07\/flex-on-rails\/"},"modified":"2006-08-07T08:48:13","modified_gmt":"2006-08-07T13:48:13","slug":"flex-on-rails","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/08\/07\/flex-on-rails\/","title":{"rendered":"Flex On Rails"},"content":{"rendered":"<p>In a Ruby on Rails web application you can do some fancy client-side effects using AJAX and <b>RJS<\/b> via remote links and remote forms.  But toggling and fading HTML elements is a far cry from <b>Rich Internet Application<\/b> controls.  In this article I will demonstrate how to connect a Flex 2 UI on the client-side with Ruby on Rails application on the server end.  This example is loosely ported from <a href=\"http:\/\/www.adobe.com\/devnet\/flex\/articles\/flex2_php.html\">Integrating Adobe Flex and PHP<\/a> by Mike Potter of Adobe.<\/p>\n<p>Flex applications can read in xml documents from a web server and display it&#8217;s contents in a tabular format.  To demonstrate this let us first define a new action that will list all the users in a rails application.<\/p>\n<pre>\ndef listusers\n  @users = User.find :all\n  render :layout =&gt; false\nend\n<\/pre>\n<p>For this action, create a listusers.rxml view.  To create an xml document that lists the known users in the system add the following xml builder code in the listusers.rxml file:<\/p>\n<pre>\nxml.users do\n  @users.each do |user|\n    xml.user do\n      xml.id user.id\n      xml.username user.user\n      xml.since user.created_at\n      xml.email user.email\n    end\n  end\nend\n<\/pre>\n<p><!--more--><br \/>\nUse your favorite browser, i.e. Firefox, to make sure that the listusers action and view are correctly working.  At this point we are ready to start developing the Flex UI.  To continue with the sample code you will need  to install the <a href=\"http:\/\/www.adobe.com\/products\/flex\/sdk\/\">Flex 2 SDK<\/a>.<\/p>\n<p>To write the Flex portion of the application we will use MXML.  MXML is like Mozilla&#8217;s XUL, basically you define UI widgets and their attributes in an XML document.  In our example, we want to display the known users in a tabular form so we will use a mx:DataGrid widget.  We will have a button from which to request the the xml document generated by the listusers rails action.  And we will require a few lines of <b>ActionScript 3.0<\/b> code to populate the mx:DataGrid with the contents from the xml document.  I&#8217;ll first post the Flex code for the UI and then try to explain each element.<\/p>\n<pre>\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;mx:Application\n\txmlns:mx=\"http:\/\/www.adobe.com\/2006\/mxml\"\n\tlayout=\"horizontal\" horizontalAlign=\"center\" verticalAlign=\"middle\"\n&gt;\n  &lt;mx:Script&gt;\n    &lt;![CDATA[\n      import mx.rpc.events.ResultEvent\n\n      private function getback( event:ResultEvent ):void {\n        dgUsers.dataProvider = event.result.users.user\n       }\n    ]]&gt;\n  &lt;\/mx:Script&gt;\n\n  &lt;mx:HTTPService\n    id=\"reqUsers\" url=\"http:\/\/localhost:3000\/mycontroller\/listusers\"\n    useProxy=\"false\" method=\"GET\" result=\"getback(event);\"\n  \/&gt;\n\n  &lt;mx:Panel title=\"System Users\" paddingBottom=\"10\" paddingTop=\"10\"\n    paddingLeft=\"10\" paddingRight=\"10\"\n  &gt;\n    &lt;mx:DataGrid id=\"dgUsers\"&gt;\n      &lt;mx:columns&gt;\n        &lt;mx:DataGridColumn headerText=\"User ID\" dataField=\"id\"\/&gt;\n        &lt;mx:DataGridColumn headerText=\"User Name\" dataField=\"username\"\/&gt;\n        &lt;mx:DataGridColumn headerText=\"User Since\" dataField=\"since\"\/&gt;\n        &lt;mx:DataGridColumn headerText=\"User Email\" dataField=\"email\"\/&gt;\n      &lt;\/mx:columns&gt;\n    &lt;\/mx:DataGrid&gt;\n    &lt;mx:Button label=\"Search\" click=\"reqUsers.send();\"\/&gt;\n  &lt;\/mx:Panel&gt;\n&lt;\/mx:Application&gt;\n<\/pre>\n<p>As  mentioned earlier, the mx:DataGrid element will render a table.  The dataField attributes in the mx:DataGridColumn element needs to match the xml elements generated by our rxml view.  The headerText attribute is used to display the column header in the table.  The click attribute on the button acts as onclick event handler.  When the &#8216;Seach&#8217; button is pressed the send() method will be executed on the element identified as reqUsers.  In this case, reqUsers represents an HTTPService object which will be used to send our request and for which rails will resolve to our listusers action.  If you examine the mx:HTTPService element you will notice the url and method attributes and the result event handler.<\/p>\n<p>And finally, lets examine the getback ActionScript method.  The users.user portion of the right hand value is related to the elements in the xml document created in rails.  In the left hand side, we use the dataProvider property of the mx:DataGrid element to set it&#8217;s data to the users returned in the xml reponse.  Stay tuned to see how to post data from Flex to rails.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/flex\" rel=\"tag\">flex<\/a>, <a href=\"http:\/\/technorati.com\/tag\/adobe+flex\" rel=\"tag\"> adobe flex<\/a>, <a href=\"http:\/\/technorati.com\/tag\/actionscript\" rel=\"tag\"> actionscript<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ruby\" rel=\"tag\"> ruby<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ruby+on+rails\" rel=\"tag\"> ruby on rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rubyonrails\" rel=\"tag\"> rubyonrails<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a Ruby on Rails web application you can do some fancy client-side effects using AJAX and RJS via remote links and remote forms. But toggling and fading HTML elements is a far cry from Rich Internet Application controls. In this article I will demonstrate how to connect a Flex 2 UI on the client-side [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[22,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-2n","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/147"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}