<?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>Webmonkey &#187; rails</title>
    <atom:link href="http://www.webmonkey.com/tag/rails/feed/" rel="self" type="application/rss+xml" />
    <link>http://www.webmonkey.com</link>
    <description>The Web Developer&#039;s Resource</description>
    <lastBuildDate>Mon, 06 May 2013 17:29:19 +0000</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <generator>http://wordpress.org/?v=3.4.2</generator>
    
    <item>
        <title>Building With Ajax and Rails</title>
        <link>http://www.webmonkey.com/2010/02/building_with_ajax_and_rails/</link>
        <comments>http://www.webmonkey.com/2010/02/building_with_ajax_and_rails/#comments</comments>
        <pubDate>Tue, 16 Feb 2010 01:45:47 +0000</pubDate>

                <dc:creator>Webmonkey Staff</dc:creator>

        <guid isPermaLink="false">http://stag.wired.com/primate/?p=769</guid>
        		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wiki]]></category>
        <description><![CDATA[So, since the last time you brushed your teeth, Ruby on Rails has only grown in popularity. The list of web applications using the exciting new web framework has grown to such an enormous size, it has exceeded the 50K per page limit of the wiki used to host it. Lesser languages like Java and [...]]]></description>

            <content:encoded><![CDATA[<!-- wpautop disabled --><p><br />

So, since the last time you brushed your teeth, Ruby on Rails has only grown in popularity. The <a href="http://wiki.rubyonrails.com/rails/pages/RealWorldUsage" class="external text" title="http://wiki.rubyonrails.com/rails/pages/RealWorldUsage" rel="nofollow">list of web applications</a> using the exciting new web framework has grown to such an enormous size, it has exceeded the 50K per page limit of the wiki used to host it. Lesser languages like Java and PHP are copying the stylish efficiency of Rails with their own frameworks like <a href="https://trails.dev.java.net/" class="external text" title="https://trails.dev.java.net/" rel="nofollow">Trails</a>, <a href="http://phpontrax.com/" class="external text" title="http://phpontrax.com/" rel="nofollow">Trax</a> and <a href="https://trac.cakephp.org/" class="external text" title="https://trac.cakephp.org/" rel="nofollow">Cake</a>.

</p><p>In the tutorial <a href="/2010/02/Ruby_on_Rails_for_Beginners" title="Tutorial:Ruby on Rails for Beginners"> Ruby on Rails for Beginners</a>, we went over the very basic basics of Ruby and Rails:what it is, why it&#8217;s so mindblowingly cool, which celebrities are using it, and so forth. As soon as the article went live, letters flooded in, offering me book contracts, movie deals and exotic snacks &#8212; I haven&#8217;t gotten so much attention since my <a href="/2010/02/Ajax_for_Beginners" title="Tutorial:Ajax for Beginners"> Ajax for Beginners</a> article. In fact, <a href="http://www.lifehacker.com/software/poll/reader-poll-what-interests-geeks-116976.php" class="external text" title="http://www.lifehacker.com/software/poll/reader-poll-what-interests-geeks-116976.php" rel="nofollow">this poll</a> from the redoubtable Lifehacker.com says that Ruby on Rails and Ajax are among the two most popular things in the world, and plainly it pays to follow the trends, so what if we combined the two of them? No, that would be excessive. You don&#8217;t want to read about that. You do? Hmmmm, OK, I suppose we can take a quick look.



</p><span id="more-769"></span>

<table id="toc" class="toc" summary="Contents"><tbody><tr><td><div id="toctitle"><h2>Contents</h2> </div>

<ol>

<li><a href="#You_Got_Ajax_In_My_Rails">You Got Ajax In My Rails</a></li>

<li><a href="#Adding_Prototype.js">Adding Prototype.js</a></li>



<li><a href="#Form_Of:A_Form.21">Form Of:A Form!</a></li>

<li><a href="#Moving_Onward">Moving Onward</a></li>

</ol>

</td></tr></tbody></table>



<p><br />

</p>

<a name="You_Got_Ajax_In_My_Rails"></a><h3> <span class="mw-headline">You Got Ajax In My Rails</span></h3>

<p>Let me just refresh my memory a little. Ajax (it seems like everyone&#8217;s calling it that now rather than my spiffy coinage, &#8220;XHR&#8221;) is the handy JavaScript technique that allows the browser to communicate with the server without reloading the current page, to do things like validate or save form input, refresh images and heaven knows what else.



</p><p>In the <a href="/2010/02/Ajax_for_Beginners" title="Tutorial:Ajax for Beginners"> Ajax for Beginners</a> article, we created those client-side dynamic functions by typing in a bunch of code by hand, but that goes against the make-it-quick-and-easy methodology of Rails. Instead, there are some pleasurably easy, automated ways to incorporate Ajax into a Rails-driven site. Rails ships with the <a href="http://prototype.conio.net/" class="external text" title="http://prototype.conio.net/" rel="nofollow">Prototype</a> JavaScript library, which provides a nice high-level toolkit for working with Ajax functions, and with <a href="http://script.aculo.us/" class="external text" title="http://script.aculo.us/" rel="nofollow">Script.aculo.us</a>, which builds additional bells and whistles onto Prototype. These two can be downloaded separately and used in non-Rails projects as well.

</p><p><br />

</p>

<a name="Adding_Prototype.js"></a><h3> <span class="mw-headline">Adding Prototype.js</span></h3>

<p>Let&#8217;s revisit the example site from the <a href="/2010/02/Ruby_on_Rails_for_Beginners" title="Tutorial:Ruby on Rails for Beginners"> Ruby on Rails for Beginners</a>  article. Maybe you&#8217;ve still got a copy of it lying around on your web server, or maybe you&#8217;d like to create it again from scratch. I&#8217;ll wait here.



</p><p>You&#8217;ll notice that the <tt>public/javascripts</tt> directory contains a file called &#8220;prototype.js&#8221; &#8212; that&#8217;s our friend Prototype! There isn&#8217;t much documentation for it, but if you read JavaScript fluently you can look at the file and see the basic methods it defines. In order to use the methods, first and foremost we need to activate them. That can be done one Ruby block at a time or for a whole page; let&#8217;s choose the latter. In the rHTML file that contains the page layout, namely <tt>app/views/layouts/bookmarks.rhtml</tt>, insert the line

</p>

<pre class="brush: js">&lt;%= javascript_include_tag&nbsp;:defaults&nbsp;%&gt;

</pre>

<p>right before the closing <tt>&nbsp;/head¢</tt> tag.

</p><p>Then we can get right down to creating our Ajax snazz. In our last session, we looked at HTTP links that used the Ruby function <tt>link_to()</tt>. Remember that? They looked like this (an example from <tt>app/views/bookmarks/list.rhtml</tt>):



</p>

<pre class="brush: js"> &lt;%= link_to 'Destroy', {:action =&gt; 'destroy',&nbsp;:id =&gt; bookmark},&nbsp;:confirm =&gt; "Are you sure?"&nbsp;%&gt;</pre>

<p>Well, <tt>link_to()</tt> has a buddy called <tt>link_to_remote()</tt>. It works in a similar fashion, except it takes an additional parameter, besides the name of the link and the action or URL it points to:a DOM element ID. The Ajax action will place any content it retrieves from the server into that element. So, if we wanted to rewrite our function above, so we could delete a bookmark from our list without reloading the whole page, we&#8217;d say:



</p>

<pre class="brush: js">&lt;%= link_to_remote 'Destroy', {:action =&gt; 'ajaxdestroy',&nbsp;:id =&gt; list,&nbsp;:update =&gt; 'bookmarks'},&nbsp;:confirm =&gt; "Are you sure?"&nbsp;%&gt;</pre>

<p>Notice the <tt>:update</tt> parameter &#8211; that&#8217;s the one that&#8217;s new. Now, when we click the Destroy link, it calls the <tt>ajaxdestroy</tt> action to delete the bookmark in question from the database, but instead of a page reload in the browser, the server will do the work behind the scenes and just return a new bookmark listing, which will be placed in the <tt>list</tt> DOM object. That DOM object corresponds to the list of bookmarks on the page. Now, that <tt>xhrdestroy</tt> action doesn&#8217;t exist yet. We have to create it, in <tt>app/controllers/bookmarks_controller.rb</tt>:



</p>

 <pre class="brush: js">

 def ajaxdestroy



  Bookmark.find(@params[:id]).destroy



  render&nbsp;:partial =&gt; "bookmarks"



 end



 </pre>

<p>The first line does the deleting:it looks at the @params array sent by the browser, which contains the ID of the bookmark to be deleted; finds the corresponding row in the database; and destroys it. Simple enough, I hope. The second line returns the response, using the very handy <a href="http://api.rubyonrails.com/classes/ActionController/Base.html#M000169" class="external text" title="http://api.rubyonrails.com/classes/ActionController/Base.html#M000169" rel="nofollow">render()</a> method. This method can be used to create and send a &#8220;partial,&#8221; or chunk of a web page, which it does here:the rendered list of bookmarks is returned and displayed without a full reload. It&#8217;d be wise to include fallback options for browsers that don&#8217;t support Ajax, but I leave that to you, my intelligent reader.

</p><p><br />

</p>

<a name="Form_Of:A_Form.21"></a><h3> <span class="mw-headline">Form Of:A Form!</span></h3>



<p>In our <a href="/2010/02/Ajax_for_Beginners" title="Tutorial:Ajax for Beginners"> Ajax for Beginners</a> lesson, we wrote a PHP/JavaScript function that would automatically grade essays and return the numeric grade in an alert window. To do the same with Rails, we&#8217;d use a method called <tt>form_remote_tag()</tt>, which submits a form to a server-side handler via XMLHTTPRequest. This is an Ajax version of the more familiar <tt>form_tag()</tt>.

</p>

 <pre class="brush: js">

 &lt;%= form_remote_tag(:url =&gt; {:action =&gt; 'grade'},&nbsp;:update



 =&gt; result)&nbsp;%&gt;



        Enter your essay here:



        &lt;%= text_area_tag "essay"&nbsp;%&gt;



        &lt;%= submit_tag "What's my grade?"&nbsp;%&gt;



        Result:



        &lt;%= text_field_tag "result"&nbsp;%&gt;



      &lt;%= end_form_tag&nbsp;%&gt;



 </pre>

<p>The user types their little essay into the text area created by <tt>text_area_tag</tt>. The form content is submitted to the controller action &#8220;grade,&#8221; which we will define in a minute. That action grades the essay and sends a numeric grade back to the form, which displays it in the &#8220;result&#8221; text field &#8212; all without reloading the page. (I know, it&#8217;s less and less thrilling every time I say that. Still a little cool though, right?) The grading action, which lives in the <tt>app/controllers</tt> directory, looks like this. Feel free to substitute your own more-rigorous, essay-grading algorithm:

</p>

 <pre class="brush: js">



 def grade



  puts params[:essay].strip.squeeze.split(/s/).size



 end



 </pre>

<p>OK, that &#8220;grading&#8221; function really just does a word count, using Ruby&#8217;s redoubtable string methods. I&#8217;ve used it to grade my students&#8217; papers though and it seems to work just fine.

</p><p><tt>puts</tt> is the Ruby equivalent of <tt>echo</tt> or <tt>print</tt>. Ruby, unlike some languages you may know, doesn&#8217;t need values to be explicitly returned from its methods. Instead, the final output of the method is automatically returned to the method&#8217;s caller. <tt>params</tt> is the array containing the entire submitted form, <tt>params[:essay]</tt> is the essay, <tt>strip</tt> removes leading and trailing white space, <tt>squeeze</tt> removes multiple repeating characters so double spaces aren&#8217;t counted twice, <tt>split</tt> breaks the string at every remaining space into an array, and <tt>size</tt> returns the number of elements in that array, i.e. the word count.



</p><p><br />

</p>

<a name="Moving_Onward"></a><h3> <span class="mw-headline">Moving Onward</span></h3>

<p>Those two methods give you plenty to work with, Ajax-on-Rails-wise (&#8220;Rajax&#8221;? Would that catch on, you think?) There are a dozen or so more, though, documented <a href="http://api.rubyonrails.com/classes/ActionView/Helpers/JavaScriptHelper.html" class="external text" title="http://api.rubyonrails.com/classes/ActionView/Helpers/JavaScriptHelper.html" rel="nofollow">here</a>. <tt>observe_field</tt> and <tt>observe_form</tt> create event handlers that make Ajax calls when a field changes. Very handy for autocompletion. <tt>draggable_element</tt> makes a DOM object draggable. It can then be dropped on an element specified with <tt>drop_receiving_element</tt>. For flashier stuff, you can check out some of the <a href="http://wiki.script.aculo.us/scriptaculous/show/Demos" class="external text" title="http://wiki.script.aculo.us/scriptaculous/show/Demos" rel="nofollow">demos</a> at script.aculo.us.



</p><p>Now that it&#8217;s so easy to create snazzy Ajaxy database-driven websites, the next step is for some of you to go out there and actually make <i>good</i> ones. I look forward to clicking your buttons and dragging your elements!

</p><p><br /><div id='linker_widget' class='contextly-widget'></div>]]></content:encoded>
            <wfw:commentRss>http://www.webmonkey.com/2010/02/building_with_ajax_and_rails/feed/</wfw:commentRss>
        <slash:comments>0</slash:comments>

        
    </item>
    
    <item>
        <title>Rails 2.1 Has Been Released</title>
        <link>http://www.webmonkey.com/2008/06/rails_2dot1_has_been_released/</link>
        <comments>http://www.webmonkey.com/2008/06/rails_2dot1_has_been_released/#comments</comments>
        <pubDate>Mon, 02 Jun 2008 15:03:22 +0000</pubDate>

                <dc:creator>Paul Adams</dc:creator>

        <guid isPermaLink="false">http://www.webmonkey.com/blog/rails21hasbeenreleased</guid>
        		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
        <description><![CDATA[The latest version of the popular web framework, long awaited in some circles, is now available, half a year or so after 2.0. Significant new features include built-in handling of time zones; improvements to caching; named_scope functionality in ActiveRecord, and more. It&#8217;s neatly synchronized with the release of Ruby 1.8.7, which includes support for SSL/TLS, [...]]]></description>

            <content:encoded><![CDATA[<p><!-- wpautop enabled --><img class="blogimg" src="http://www.rubyonrails.org/images/rails.png" style="float:right;">The latest version of the popular web framework, long awaited in some circles, is now available, half a year or so after 2.0.</p>
<p>Significant new features include built-in handling of time zones; improvements to caching; <code>named_scope</code> functionality in ActiveRecord, and <a href="http://weblog.rubyonrails.org/2008/6/1/rails-2-1-time-zones-dirty-caching-gem-dependencies-caching-etc">more</a>.</p>
<p>It&#8217;s neatly synchronized with the release of <a href="http://www.ruby-lang.org/en/">Ruby 1.8.7</a>, which includes support for SSL/TLS, experimental provision for passing a block to a proc, and quite a bit <a href="http://svn.ruby-lang.org/repos/ruby/tags/v1_8_7/NEWS">more</a>.</p>
<p><strong>See Also:</strong><br/></p>
<ul>
<li><a href="http://blog.wired.com/monkeybites/2007/05/hello_im_ruby_o.html#previouspost">Hello, I&#39;m Ruby On Rails</a></li>
<p><a href="http://blog.wired.com/monkeybites/2007/12/ruby-on-rails-2.html">Ruby on Rails 2.0</a></li>
</ul>
<div id='linker_widget' class='contextly-widget'></div>
]]></content:encoded>
            <wfw:commentRss>http://www.webmonkey.com/2008/06/rails_2dot1_has_been_released/feed/</wfw:commentRss>
        <slash:comments>0</slash:comments>

        
    </item>
    </channel>
</rss>
