<?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>Vinsol - Leading Ruby on Rails Development and Consulting Firm in India &#187; ajax</title>
	<atom:link href="http://vinsol.com/blog/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://vinsol.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 26 Jul 2010 08:37:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby on Rails Caching And JavaScript Techniques</title>
		<link>http://vinsol.com/blog/2009/09/07/rails-caching-and-javascript-techniques/</link>
		<comments>http://vinsol.com/blog/2009/09/07/rails-caching-and-javascript-techniques/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 06:47:20 +0000</pubDate>
		<dc:creator>sid</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://vinsol.com/blog/?p=704</guid>
		<description><![CDATA[Cross posted from darthsid
While implementing caching in a recent rails project I came across some typical caching issues. In a lot of pages the content is same for all users but certain components in them have user specific actions. As an example, I have a page listing all public messages that users have posted(similar to [...]


Related posts:<ol><li><a href='http://vinsol.com/blog/2010/03/29/jquery-full-calendar-with-rails/' rel='bookmark' title='Permanent Link: Jquery Full Calendar with Ruby on Rails'>Jquery Full Calendar with Ruby on Rails</a> <small>Contrary to popular belief, working on a client project gives...</small></li>
<li><a href='http://vinsol.com/blog/2009/10/29/integrating-yahoo-boss-with-your-rails-application/' rel='bookmark' title='Permanent Link: Integrating Yahoo! BOSS with your ruby on rails application'>Integrating Yahoo! BOSS with your ruby on rails application</a> <small>What is Yahoo! BOSS? Yahoo developer website cites it as:...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Cross posted from <a title="Sid's Blog" href="http://darthsid.com/blog">darthsid</a></p>
<p>While implementing caching in a recent rails project I came across some typical caching issues. In a lot of pages the content is same for all users but certain components in them have user specific actions. As an example, I have a page listing all public messages that users have posted(similar to the <a href="http://twitter.com/public_timeline">public timeline</a> in <a href="http://twitter.com">twitter</a>) but actions on those messages are user specific(eg: only owner or admin can delete a message). Also, most of these actions use ajax and the rails <a href="http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention">authenticity token</a> in them also gets cached resulting in subsequent failures if the session changes. Another issue was that the timestamps in most pages is fuzzy and they become irrelevant if a page gets cached for too long. I could have created separate caches for each user but if the user base really grows managing the caches would become a nightmare and that would still not solve the authenticity token and the timestamp problem. The simplest solution was to use JavaScript, more specifically <a href="http://jquery.com">jQuery</a>.<br />
<span id="more-704"></span><br />
I have been a big fan of jQuery since I started using it about a year and a half ago and try to use it in all my projects. There is an excellent Rails plugin called <a href="http://ennerchi.com/projects/jrails">jRails</a> that replaces Prototype with jQuery and provides all the default Rails helpers for JavaScript making jQuery even more tempting to use. The examples I use below all use jQuery.</p>
<p><big><big><strong>1. Handling User Specific Components</strong></big></big></p>
<p>Taking the delete action as explained in the example above, the code in the cached view looks something like this:</p>
<pre class="brush: ruby;">
&amp;lt;% if logged_in? %&amp;gt;
&amp;lt;li class=&amp;quot;delete &amp;lt;%= 'only_' + (message.owner.login) + '_delete_allowed' %&amp;gt;&amp;quot;&amp;gt;
  &amp;lt;%= link_to_remote &amp;quot;Delete&amp;quot;,
    :url =&amp;gt; user_message_path(current_user, message)
    :method =&amp;gt; :delete,
    :confirm =&amp;gt; &amp;quot;Are you sure you wish to delete this message?&amp;quot;,
    :html =&amp;gt; { :title =&amp;gt; &amp;quot;Delete Post&amp;quot; } %&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;% end %&amp;gt;
</pre>
<p>This will create an li element with class &#8220;delete&#8221; and &#8220;only_kratos_delete_allowed&#8221; if the username of the message owner is &#8220;kratos&#8221;</p>
<p>In the non-cached part of the view I have the following code:</p>
<pre class="brush: ruby;">
&amp;lt;% if logged_in? %&amp;gt;
  &amp;lt;% if current_user.is_admin? %&amp;gt;
    &amp;lt;%= javascript_tag &amp;quot;$(document).ready(function() { MessageView.removeInvalidDeteteButtons('only_#{current_user.login}_delete_allowed', 'true'); });&amp;quot; %&amp;gt;
  &amp;lt;% else %&amp;gt;
    &amp;lt;%= javascript_tag &amp;quot;$(document).ready(function() { MessageView.removeInvalidDeteteButtons('only_#{current_user.login}_delete_allowed'); });&amp;quot; %&amp;gt;
  &amp;lt;% end %&amp;gt;
&amp;lt;% end %&amp;gt;
</pre>
<p>If the current user is an admin I pass the JavaScript function an additional parameter.</p>
<p>In the application.js file I have the following code:</p>
<pre class="brush: jscript;">
var MessageView = {
  removeInvalidDeteteButtons: function(element_class, admin) {
    if (admin == undefined)
    {
      $('.delete').each(function() { if (!$(this).hasClass(element_class)) { $(this).remove(); } });
    }
  }
}
</pre>
<p>If the second parameter is passed(in the case of an admin) the function does nothing, else it iterates over all li elements with class &#8220;delete&#8221; and removes all elements that do not also have, in this case, the &#8220;only_kratos_delete_allowed&#8221; class.</p>
<p><big><big><strong>2. Handling Rails Authenticity Token</strong></big></big></p>
<p>This part describes how to take care of the authenticity token problem:</p>
<p>I added the following code to the layout:</p>
<pre class="brush: ruby;">
&amp;lt;%= javascript_tag &amp;quot;var AUTH_TOKEN = #{form_authenticity_token.inspect};&amp;quot; if protect_against_forgery? %&amp;gt;
</pre>
<p>This code creates a JavaScript variable named &#8220;AUTH_TOKEN&#8221; that contains the current authentication token. Since this section is not cached it always get the correct token.</p>
<p>Next I added the following code to application.js:</p>
<pre class="brush: jscript;">
$(document).ajaxSend(function(event, request, settings) {
  if (settings.type == 'get' || settings.type == 'GET' || typeof(AUTH_TOKEN) == &amp;quot;undefined&amp;quot;) return;
  var authTokenRegExp = /authenticity_token=\w{40}/
  settings.data = settings.data || &amp;quot;&amp;quot;;
  if (authTokenRegExp.test(settings.data))
  {
    settings.data=settings.data.replace(authTokenRegExp, &amp;quot;authenticity_token=&amp;quot; + encodeURIComponent(AUTH_TOKEN));
  }
  else
  {
    settings.data += (settings.data ? &amp;quot;&amp;amp;&amp;quot; : &amp;quot;&amp;quot;) + &amp;quot;authenticity_token=&amp;quot; + encodeURIComponent(AUTH_TOKEN);
  }
})
</pre>
<p>This code is a slight modification of the code posted <a href="http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery">here</a>. <a href="http://docs.jquery.com/Ajax/ajaxSend">ajaxSend</a> is jQuery function that is executed before every ajax request is sent and I use it here to replace or append the authenticity token to the request, unless the request is GET or the AUTH_TOKEN variable is not defined.</p>
<p><big><big><strong>3. Handling Fuzzy Timestamps</strong></big></big></p>
<p>The code for timestamps in the cached view looks like this:</p>
<pre class="brush: ruby;">
&amp;lt;span&amp;gt;
  &amp;lt;%= process_message_body_timestamp(message) %&amp;gt;
&amp;lt;/span&amp;gt;
</pre>
<p>And the helper code is:</p>
<pre class="brush: ruby;">
def process_message_body_timestamp(message)
  link_to &amp;quot;#{message.created_at}&amp;quot;, show_message_url(message), :class =&amp;gt; &amp;quot;timestamps&amp;quot;
end
</pre>
<p>I put the following code in the layout(non-cached):</p>
<pre class="brush: ruby;">
&amp;lt;%= javascript_tag &amp;quot;$(document).ready(function() { CustomDate.datify(#{get_custom_date_arguments}) });&amp;quot; %&amp;gt;
</pre>
<p>The get_custom_date_arguments helper is defined in application helper and returns a string containing the current time arguments needed for the JavaScript function:</p>
<pre class="brush: ruby;">
def get_custom_date_arguments
  current_time = Time.zone.now
  &amp;quot;#{current_time.year},#{current_time.month-1},#{current_time.day},#{current_time.hour},#{current_time.min},#{current_time.sec}&amp;quot;
end
</pre>
<p>I put the following code in application.js:</p>
<pre class="brush: jscript;">
var CustomDate = {
  datify: function(current_utc_year,
                   current_utc_month,
                   current_utc_day,
                   current_utc_hour,
                   current_utc_minute,
                   current_utc_second) {
    $('.timestamps').each(function() {
      $(this).html(CustomDate.humane_date($(this).html(),
                                          current_utc_year,
                                          current_utc_month,
                                          current_utc_day,
                                          current_utc_hour,
                                          current_utc_minute,
                                          current_utc_second)).removeClass('timestamps')
    })
  },

  humane_date: function(date_str, current_utc_year, current_utc_month, current_utc_day, current_utc_hour, current_utc_minute, current_utc_second) {
    var time_formats = [
                        [60, 'less than a minute ago'],
                        [90, '1 minute'], // 60*1.5
                        [3600, 'minutes', 60], // 60*60, 60
                        [5400, '1 hour'], // 60*60*1.5
                        [86400, 'hours', 3600], // 60*60*24, 60*60
                        [129600, '1 day'], // 60*60*24*1.5
                        [604800, 'days', 86400], // 60*60*24*7, 60*60*24
                        [907200, '1 week'], // 60*60*24*7*1.5
                        [2628000, 'weeks', 604800], // 60*60*24*(365/12), 60*60*24*7
                        [3942000, '1 month'], // 60*60*24*(365/12)*1.5
                        [31536000, 'months', 2628000], // 60*60*24*365, 60*60*24*(365/12)
                        [47304000, '1 year'], // 60*60*24*365*1.5
                        [3153600000, 'years', 31536000], // 60*60*24*365*100, 60*60*24*365
                        [4730400000, '1 century'], // 60*60*24*365*100*1.5
                      ];

    var time = ('' + date_str).replace(/-/g,&amp;quot;/&amp;quot;).replace(/[TUTC]/g,&amp;quot; &amp;quot;),
        dt = new Date

    dt.setUTCFullYear(current_utc_year, current_utc_month, current_utc_day)
    dt.setUTCHours(current_utc_hour, current_utc_minute, current_utc_second)

    var seconds = ((dt - new Date(time) + (dt.getTimezoneOffset() * 60000)) / 1000),
        token = ' ago',
        prepend = '',
        i = 0,
        format;

    if (seconds &amp;lt; 0) {
      seconds = Math.abs(seconds);
      token = '';
      prepend = 'in ';
    }

    while (format = time_formats[i++]) {
      if (seconds &amp;lt; format[0]) {
        if (format.length == 2) {
          return (i&amp;gt;1?prepend:'') + format[1] + (i &amp;gt; 1 ? token : ''); // Conditional so we don't return Just Now Ago
        }
        else {
            return prepend + Math.round(seconds / format[2]) + ' ' + format[1] + (i &amp;gt; 1 ? token : '');
        }
      }
    }

    // overflow for centuries
    if(seconds &amp;gt; 4730400000) {
      return Math.round(seconds / 4730400000) + ' Centuries' + token;
    }

    return date_str;
  }
}
</pre>
<p>The datify function iterates over all timestamps(elements with class &#8220;timestamps&#8221;) and replaces them with fuzzy timestamps. The humane_date function(yanked from <a href="http://stackoverflow.com/questions/168924/how-to-render-contextual-difference-between-two-timestamps-in-javascript">here</a>) generates the actual fuzzy timestamps.</p>


<p>Related posts:<ol><li><a href='http://vinsol.com/blog/2010/03/29/jquery-full-calendar-with-rails/' rel='bookmark' title='Permanent Link: Jquery Full Calendar with Ruby on Rails'>Jquery Full Calendar with Ruby on Rails</a> <small>Contrary to popular belief, working on a client project gives...</small></li>
<li><a href='http://vinsol.com/blog/2009/10/29/integrating-yahoo-boss-with-your-rails-application/' rel='bookmark' title='Permanent Link: Integrating Yahoo! BOSS with your ruby on rails application'>Integrating Yahoo! BOSS with your ruby on rails application</a> <small>What is Yahoo! BOSS? Yahoo developer website cites it as:...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2009/09/07/rails-caching-and-javascript-techniques/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Two best online API/Rails API</title>
		<link>http://vinsol.com/blog/2007/11/05/two-best-online-apirails-api/</link>
		<comments>http://vinsol.com/blog/2007/11/05/two-best-online-apirails-api/#comments</comments>
		<pubDate>Mon, 05 Nov 2007 08:08:17 +0000</pubDate>
		<dc:creator>Akhil Bansal</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[RubyonRails]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://webonrails.com/2007/11/05/two-best-online-apirails-api/</guid>
		<description><![CDATA[Today I found two best API sites. One is http://www.gotapi.com.
This site provides APIs of almost all languages.
The other API site which is only focused on Rails is http://www.railsbrain.com/. I like this most. You can also download this API. It is AJAX based fast, useful.




No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Today I found two best API sites. One is <a href="http://www.gotapi.com">http://www.gotapi.com</a>.<br />
This site provides APIs of almost all languages.</p>
<p>The other API site which is only focused on Rails is <a href="http://www.railsbrain.com/">http://www.railsbrain.com/</a>. I like this most. You can also download this API. It is AJAX based fast, useful.
</p>
]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2007/11/05/two-best-online-apirails-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sample Rails Application &#8211; A demo for the ajax based drag drop tree in rubyonrails</title>
		<link>http://vinsol.com/blog/2006/11/26/sample-rails-application-a-demo-for-the-ajax-based-drag-drop-tree-in-rubyonrails/</link>
		<comments>http://vinsol.com/blog/2006/11/26/sample-rails-application-a-demo-for-the-ajax-based-drag-drop-tree-in-rubyonrails/#comments</comments>
		<pubDate>Sun, 26 Nov 2006 09:44:31 +0000</pubDate>
		<dc:creator>Sur Max</dc:creator>
				<category><![CDATA[RubyonRails]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax tree]]></category>
		<category><![CDATA[drag drop tree]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://ajaxonrails.wordpress.com/2006/11/26/ajaxonrailsdragdroptree/</guid>
		<description><![CDATA[I have provided the source code of the ajax based drag drop tree in rubyonrails in one of my previous posts.
I found some of the people are getting problems to incorporate the code into their running applications so i am providing a sample rails application in which all the code for tree is already been [...]


Related posts:<ol><li><a href='http://vinsol.com/blog/2009/11/16/11-things-to-consider-before-deploying-your-rails-application/' rel='bookmark' title='Permanent Link: 11 Things to Consider Before Deploying Your Ruby on  Rails Application'>11 Things to Consider Before Deploying Your Ruby on  Rails Application</a> <small>At VinSol, we have been developing and deploying Rails applications...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'>
<p>I have provided the <a href="http://ajaxonrails.wordpress.com/2006/08/18/sorce-code-for-ajax-based-drag-drop-navigation-tree-for-rails/"><strong>source code</strong></a> of the ajax based drag drop tree in rubyonrails in one of my previous posts.<br />
I found some of the people are getting problems to incorporate the code into their running applications so i am providing a sample rails application in which all the code for tree is already been placed well.<br />
However the code written seems to be lagged behind the current trends followed in rails development coz of the fire growth of rails itself, but its simply that when i wrote this tree i was very new to rails so you may find the code looks like an old wine but still tastes good to go with.</p>
<p>CHECK THIS OUT&#8230;</p>
<p><strong>Four simple steps to make the tree working&#8230;</strong></p>
<ol>
<li><a href="http://rubyforge.org/frs/download.php/15381/ajaxtree.zip"><strong>DOWNLOAD</strong></a> the sample application. (let me know if you are getting any error in downloading the application.)</li>
<li>Create a <strong>test</strong> database in mysql <strong>or</strong> modify the file <strong>/config/database.yml</strong> according to the database and user u need.</li>
<li>Run the command
<pre>ajaxtree&gt; rake db:migrate</pre>
<p>from the application root.</li>
<li>Run the application server by running
<pre>ajaxtree&gt; ruby script/server</pre>
<p>and watch the working tree at <strong>http://localhost:3000</strong></li>
</ol>
</div>


<p>Related posts:<ol><li><a href='http://vinsol.com/blog/2009/11/16/11-things-to-consider-before-deploying-your-rails-application/' rel='bookmark' title='Permanent Link: 11 Things to Consider Before Deploying Your Ruby on  Rails Application'>11 Things to Consider Before Deploying Your Ruby on  Rails Application</a> <small>At VinSol, we have been developing and deploying Rails applications...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/11/26/sample-rails-application-a-demo-for-the-ajax-based-drag-drop-tree-in-rubyonrails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uploading files using AJAX</title>
		<link>http://vinsol.com/blog/2006/11/20/uploading-files-using-ajax/</link>
		<comments>http://vinsol.com/blog/2006/11/20/uploading-files-using-ajax/#comments</comments>
		<pubDate>Mon, 20 Nov 2006 18:26:18 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://webonrails.com/?p=60</guid>
		<description><![CDATA[There is no way to upload files using AJAX. How Gmail does it then? Well, the answer is using &#60;iframe&#62;. Here is a post that  describe this technique in detail. Infact we are using this technique from months, obviously inspired by Gmail. ;-)
update: Technically speaking, it’s not Ajax. Just an iframe hack. You are [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p><strong>There is no way to upload files using AJAX.</strong> How Gmail does it then? Well, the answer is using &lt;iframe&gt;. <a href="http://kpumuk.info/ruby-on-rails/in-place-file-upload-with-ruby-on-rails/">Here</a> is a post that  describe this technique in detail. Infact we are using this technique from months, obviously inspired by Gmail. ;-)</p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/11/20/uploading-files-using-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Animator.js: A javascript library by Bernie Sumption</title>
		<link>http://vinsol.com/blog/2006/11/03/animatorjs-a-javascript-library-by-bernie-sumption/</link>
		<comments>http://vinsol.com/blog/2006/11/03/animatorjs-a-javascript-library-by-bernie-sumption/#comments</comments>
		<pubDate>Fri, 03 Nov 2006 19:14:24 +0000</pubDate>
		<dc:creator>Akhil Bansal</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://webonrails.com/?p=55</guid>
		<description><![CDATA[While surfing on net I came across   a javascript library(animator.js)  based on  prototype library written by Bernie Sumption.
I found this library a bit interesting. One that appeals me is the ability to define start state and end state through CSS
See in action here.




Related posts:<ol><li><a href='http://vinsol.com/blog/2009/09/07/rails-caching-and-javascript-techniques/' rel='bookmark' title='Permanent Link: Ruby on Rails Caching And JavaScript Techniques'>Ruby on Rails Caching And JavaScript Techniques</a> <small>Cross posted from darthsid While implementing caching in a recent...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>While surfing on net I came across   a javascript library(animator.js)  based on  prototype library written by <a title="Berne Sumption" href="http://berniecode.com/">Bernie Sumption</a>.</p>
<p>I found this library a bit interesting. One that appeals me is the ability to define start state and end state through CSS</p>
<p>See in action <a href="http://berniecode.com/writing/animator.html">here</a>.</p>


<p>Related posts:<ol><li><a href='http://vinsol.com/blog/2009/09/07/rails-caching-and-javascript-techniques/' rel='bookmark' title='Permanent Link: Ruby on Rails Caching And JavaScript Techniques'>Ruby on Rails Caching And JavaScript Techniques</a> <small>Cross posted from darthsid While implementing caching in a recent...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/11/03/animatorjs-a-javascript-library-by-bernie-sumption/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Source Code For Ajax Based Drag Drop Navigation Tree in Ruby on Rails &#8211; the tree works well with firefox and IE-6</title>
		<link>http://vinsol.com/blog/2006/08/18/source-code-for-ajax-based-drag-drop-navigation-tree-in-ruby-on-rails-the-tree-works-well-with-firefox-and-ie-6/</link>
		<comments>http://vinsol.com/blog/2006/08/18/source-code-for-ajax-based-drag-drop-navigation-tree-in-ruby-on-rails-the-tree-works-well-with-firefox-and-ie-6/#comments</comments>
		<pubDate>Fri, 18 Aug 2006 12:02:35 +0000</pubDate>
		<dc:creator>Sur Max</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax tree]]></category>
		<category><![CDATA[drag drop tree]]></category>
		<category><![CDATA[navigation tree]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">https://ajaxonrails.wordpress.com/2006/08/18/sorce-code-for-ajax-based-drag-drop-navigation-tree-for-rails/</guid>
		<description><![CDATA[Ajaxified Drag Drop Tree in RoR
CASE STUDY
I m providing a very generalized use case where the tree fits in a good position. Here it is&#8230;
Consider a model Item, a controller Items. Item model is using a fabulous acts_as_tree and we are going to put a seed for Item to grow it in an ajax tree [...]


Related posts:<ol><li><a href='http://vinsol.com/blog/2009/09/07/rails-caching-and-javascript-techniques/' rel='bookmark' title='Permanent Link: Ruby on Rails Caching And JavaScript Techniques'>Ruby on Rails Caching And JavaScript Techniques</a> <small>Cross posted from darthsid While implementing caching in a recent...</small></li>
<li><a href='http://vinsol.com/blog/2009/10/29/integrating-yahoo-boss-with-your-rails-application/' rel='bookmark' title='Permanent Link: Integrating Yahoo! BOSS with your ruby on rails application'>Integrating Yahoo! BOSS with your ruby on rails application</a> <small>What is Yahoo! BOSS? Yahoo developer website cites it as:...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'>
<p><strong>Ajaxified Drag Drop Tree in RoR</strong><br />
<strong><span>CASE STUDY</span></strong></p>
<p>I m providing a very generalized use case where the tree fits in a good position. Here it is&#8230;</p>
<p>Consider a model <strong>Item</strong>, a controller <strong>Items</strong>. Item model is using a fabulous <strong>acts_as_tree</strong> and we are going to put a seed for Item to grow it in an ajax tree <img src='http://ajaxonrails.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> &#8230; Ok no more non-code talk. So, lets start the code now&#8230;</p>
<p>==========================================================<br />
I have also incorporated the code into a sample application which you can directly check out and try the tree yourself if you find it a<br />
headache to add the following code in a number of described files.<br />
So, here is the <a href="http://ajaxonrails.wordpress.com/2006/11/26/ajaxonrailsdragdroptree/"><b>Sample Tree Application</b></a></p>
<p>or you can try to code yourself as&#8230;</p>
<p>Create a sample rails application say <b>treeapp</b> by running</p>
<pre>
rails treeapp
</pre>
<p>from the command prompt.<br />
Now simply change your directiry into just created <b>treeapp</b> and make sure that you are in the directory <b>treeapp</b></p>
<p>Now configure the database settings for this application by modifying the file <b>/config/database.yml</b> as &#8230;</p>
<pre>
development:
  adapter: mysql
  database: tree_dev
  username: root
  password: root
  host: localhost
</pre>
<p>Here it simply shows that you have a mysql database named <b>tree_dev</b> and a user <b>root</b> with password <b>root</b> can access this database. So make sure about these settings.</p>
<p>From the command prompt in application root(i.e. you are in the directory <b>treeapp</b>) run this command to generate the model <strong>Item</strong>&#8230;</p>
<pre>
treeapp&gt; ruby script/generate model item
</pre>
<p>Add the following code to the file <strong>app/models/itme.rb</strong></p>
<pre>
class Item &lt; ActiveRecord::Base

  acts_as_tree
  validates_presence_of :name
  attr_accessor :style

  def self.roots
    self.find(:all, :conditions=&gt;["parent_id = ?", 0])
  end

  def level
    self.ancestors.size
  end
end</pre>
<p>This simply shows that you should have a table named <strong>tems</strong> in your database&#8230;<br />
<strong>so why we havnt mentioned it earlier ? </strong><br />
Thats the thing which will make you feel an <strong>agile web development</strong>.<br />
Now look at the directory <strong>db/migrate</strong>and a you will find a file named as <strong>db/migrate/001_create_items.rb</strong><br />
Add the following code to this file <strong>001_create_items.rb</strong><br />
Here we are creating our database table and also adding some initial data to work with.</p>
<pre>
class CreateItems &lt; ActiveRecord::Migration
  def self.up
      create_table "items", :force =&gt; true do |t|
         t.column "name", :string
         t.column "created_at", :datetime
         t.column "parent_id", :integer, :default =&gt; 0, :null =&gt; false
      end
      %w(item1 item2 item3 item4 item5).each do |name|
            parent = Item.new(:name=&gt;name)
            parent.save
            Item.create(:name=&gt;name+".1", :parent_id=&gt;parent.id)
            Item.create(:name=&gt;name+".2", :parent_id=&gt;parent.id)
            Item.create(:name=&gt;name+".3", :parent_id=&gt;parent.id)
       end
 end

 def self.down
   drop_table :items
 end

end</pre>
<p>Now from the command line from the root of your application run the following command to have a table named <strong>Item</strong> in your database with some initial data.</p>
<pre>
  treeapp&gt; rake db:migrate</pre>
<p>Before we start handling our views and controller part just have a smart small image named as <strong>drag.gif</strong> in your public/images directory that we will use as a handle to drag the nodes. So, now you can see a small image at <strong>public/images/drag.gif</strong>, cool !.<br />
Now from the command line from the root of your application run the following command to create a controller &#8230;</p>
<pre>
treeapp&gt; ruby script/generate controller items show</pre>
<p>Make sure that now you have the files <strong>app/controllers/items_controller.rb</strong> and <strong>app/views/items/show.rhtml</strong>.<br />
Add the following code in the file <strong>app/controllers/items_controller.rb</strong></p>
<pre>
class ItemsController &lt; ApplicationController

  def show
    @items = Item.find(:all)
    @item = Item.find(:first)
    # select according to your choice...
    #this item will be selected node by default in the tree when it will first be loaded.
  end

  def display_clicked_item
    # this action will handle the two way syncronization...all the tree nodes(items) will be linked
    # to this action to show the detailed item on the left of the tree when the item is clicked
    # from the tree
    if request.xhr?
      @item = Item.find(params[:id]) rescue nil
      if @item
        # the code below will render all your RJS code inline and
        # u need not to have any .rjs file, isnt this interesting
        render :update do |page|
          page.hide "selected_item"
          page.replace_html "selected_item", :partial=&gt;"items/item", :object=&gt;@item
          page.visual_effect 'toggle_appear', "selected_item"
        end
      else
        return render :nothing =&gt; true
      end
    end
  end

  def sort_ajax_tree
    if request.xhr?
      if @item = Item.find(params[:id].split("_").first) rescue nil
        parent_item = Item.find(params[:parent_id])
        render :update do |page|
          @item.parent_id = parent_item.id
          @item.save
          @items=Item.find(:all)
          page.replace_html "ajaxtree", :partial=&gt;"items/ajax_tree", :object=&gt;[@item,@items]
          page.hide "selected_item"
          page.replace_html "selected_item", :partial=&gt;"items/item", :object=&gt;@item
          page.visual_effect 'toggle_appear', "selected_item"
        end
      else
        return render :nothing =&gt; true
      end
    end
  end

end</pre>
<p>Add the following code in the file <strong>app/views/items/show.rhtml</strong></p>
<pre>
&lt;h2&gt;<strong>Ajax Tree Application</strong>&lt;/h2&gt;

&lt;div id=&#8221;ajaxtree&#8221; style=&#8221;width:40%;float:left;&#8221;&gt;
 &lt;%= render :partial=&gt;&#8217;items/ajax_tree&#8217;, :object=&gt;[@item,@items] %&gt;
&lt;/div&gt;

&lt;div id=&#8221;selected_item&#8221;&gt;
 &lt;%= render :partial=&gt;&#8217;items/item&#8217;, :object=&gt;@item %&gt;
&lt;/div&gt;</pre>
<p>Add the following code in the file <strong>app/views/items/_item.rhtml</strong></p>
<pre>
&lt;% if @item %&gt;
  &lt;h2&gt;Selected Item is &lt;%=h @item.name%&gt; &lt;/h2&gt;
&lt;% else %&gt;
  Item not found
&lt;% end %&gt;</pre>
<p>Add the following code in the file <strong>app/views/items/_ajax_tree.rhtml</strong></p>
<pre>
&lt;script type="text/javascript"&gt;

function toggleDiv()
{
  Element.toggle('mytree');
  Element.toggle('expanded');
  Element.toggle('collapsed');
  return false;
}
function showDrag()
{
  var drag_images = $$('img.drag_image');
  drag_images.all(function(value,index){return value.style.display='inline';});
  Element.toggle('done');
  Element.toggle('reorder');
  return false;
}
function hideDrag()
{
  var drag_images = $$('img.drag_image');
  drag_images.all(function(value,index){return value.style.display='none';});
  Element.toggle('done');
  Element.toggle('reorder');
  return false;
}
&lt;/script&gt;

&lt;style&gt;

.mytree{padding:0 0 0 0px;}

.mytree li {padding:2 0 0 3px;}

.outer_tree_element{margin:0 0 0 10px;}

.inner_tree_element{margin:5px 0 0 10px;}

.mytree a{text-decoration:none; font-size:13px; color:black;}

.mytree a:hover{background-color:lightblue;}

.mytree label{font-weight:normal;}

.highlighted{background-color:lightblue;}

.normal{background-color:white;}

.drag_image{border:0px;}

&lt;/style&gt;

&lt;div id="mytree" class="mytree"&gt;

  &lt;% @ancestors = @item.ancestors.collect{|parent| parent.id} if @item.has_parent? %&gt;
  &lt;% @items = Item.find(:all) %&gt;
  &lt;%= get_tree_data(@items, 0){|n|
      link_to_remote(n.name,
      :url=&gt;{:controller=&gt;'items', :action=&gt;'display_clicked_item', :id=&gt;n.id},
  :loading=&gt;"Element.show('tree_indicator')",
  :complete=&gt;"Element.hide('tree_indicator')"
  )}
  %&gt;

  &lt;% @items.each do |node| %&gt;
  &lt;%= draggable_element node.id.to_s+'_tree_div',:revert=&gt;true,:snap=&gt;false, :handle=&gt;"'#{node.id.to_s}_drag_image'" %&gt;
  &lt;%= drop_receiving_element node.id.to_s+'_tree_div',
      :accept=&gt;'inner_tree_element',
  :url=&gt;{:controller=&gt;'items',:action=&gt;'sort_ajax_tree', :parent_id=&gt;node.id,:id=&gt;nil},
  :loading=&gt;"Element.show('sort_tree_indicator')",
  :complete=&gt;"Element.hide('sort_tree_indicator')"
  %&gt;

  &lt;% end %&gt;

  &lt;%= image_tag 'indicator.gif', :id=&gt;'tree_indicator', :style=&gt;'display:none' %&gt;
  &lt;%= image_tag 'indicator.gif', :id=&gt;'sort_tree_indicator', :style=&gt;'display:none' %&gt;
&lt;/div&gt;

&lt;script type="text/javascript"&gt;

  var selected_el = document.getElementById('&lt;%=@item.id%&gt;_tree_item');
  selected_el.className='highlighted';

  function toggleMyTree(id)
  {
  Element.toggle(id+'collapsed');
  Element.toggle(id+'expanded');
  Element.toggle(id+'children');
  return false;
  }
  function toggleBackground(el)
  {
  // using collection proxies to change the background
  var highlighted_el = $$("span.highlighted");
  highlighted_el.all(function(value,index){return value.className='normal'});

  el.className='highlighted';
  selected_el = el;
  return false;
  }
  function openMyTree(id)
  {
  Element.hide(id+'collapsed');
  Element.show(id+'expanded');
  Element.show(id+'children');
  return false;
  }

&lt;/script&gt;</pre>
<p>As you can see in the above file we have used some indicator and toggle images. So you will be required to have three more images in the directory <strong>public/images/</strong>.<br />
Here is the small description about these images&#8230;</p>
<ul>
<li> An indicator image that will be displayed at the bottom of the tree whenever a tree node is clicked. You can select from a number of <a href="http://www.napyfab.com/ajax-indicators/"><strong>Ajax Inidicators</strong></a>available on the web. Select one indicator image and save in your app with the name <strong>indicator.gif</strong>. So, now make sure that you can see the image at <strong>public/images/indicator.gif</strong></li>
<li> Second, we need to have a small image with <strong>+</strong> sign. That will be used to toggle the tree. save it as <strong>public/images/collapsed.gif</strong></li>
<li> Similarly, an image with <strong>-</strong> sign. Save it as <strong>public/images/expanded.gif</strong></li>
</ul>
<p>We have to include the prototype and scriptaculous javascript  libraries in the application.<br />
So just manually create a layout file <b>app/views/layouts/application.rhtml</b> and add the following code in the file <b>application.rhtml</b></p>
<pre>
&lt;html&gt;
  &lt;head&gt;
    &lt;%= javascript_include_tag :defaults %&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;%= @content_for_layout %&gt;
  &lt;/body&gt;

&lt;/html&gt;
</pre>
<p>Now the last but the most importatnt&#8230;The recursion to obtain the tree.<br />
Add the following code in the file <strong>app/helpers/application_helper.rb</strong></p>
<pre>
module ApplicationHelper

  def get_tree_data(tree, parent_id)
    ret = "&lt;div class='outer_tree_element' &gt;"
    tree.each do |node|
      if node.parent_id == parent_id
        node.style = (@ancestors and @ancestors.include?(node.id))? 'display:inline' : 'display:none'
        display_expanded = (@ancestors and @ancestors.include?(node.id))? 'inline' : 'none'
        display_collapsed = (@ancestors and @ancestors.include?(node.id))? 'none' : 'inline'
        ret += "&lt;div class='inner_tree_element' id='#{node.id}_tree_div'&gt;"
        if node.has_children?
          ret += "&lt;img id='#{node.id.to_s}expanded' src='/images/expanded.gif' onclick='javascript: return toggleMyTree(&#92;"#{node.id}&#92;"); ' style='display:#{display_expanded}; cursor:pointer;'  /&gt;  "
          ret += "&lt;img style='display:#{display_collapsed}; cursor:pointer;'  id='#{node.id.to_s}collapsed' src='/images/collapsed.gif' onclick='javascript: return toggleMyTree(&#92;"#{node.id.to_s}&#92;"); '  /&gt;  "
        end

        ret += " &lt;img src='/images/drag.gif' style='cursor:move' id='#{node.id}_drag_image' align='absmiddle' class='drag_image' /&gt; "

        ret += "&lt;span id='#{node.id}_tree_item'&gt;"
        ret += yield node
        ret += "&lt;/span&gt;"
        ret += "&lt;span id='#{node.id}children' style='#{node.style}' &gt;"
        ret += get_tree_data(node.children, node.id){|n| yield n}
        ret += "&lt;/span&gt;"
        ret += "&lt;/div&gt;"
      end
    end
    ret += "&lt;/div&gt;"
    return ret
  end
end</pre>
<p>Now you can check the tree functionality at <strong>http://localhost:3000/items/show</strong>.. assuming that you are running your server on port 3000. njoy!!</p>
</div>


<p>Related posts:<ol><li><a href='http://vinsol.com/blog/2009/09/07/rails-caching-and-javascript-techniques/' rel='bookmark' title='Permanent Link: Ruby on Rails Caching And JavaScript Techniques'>Ruby on Rails Caching And JavaScript Techniques</a> <small>Cross posted from darthsid While implementing caching in a recent...</small></li>
<li><a href='http://vinsol.com/blog/2009/10/29/integrating-yahoo-boss-with-your-rails-application/' rel='bookmark' title='Permanent Link: Integrating Yahoo! BOSS with your ruby on rails application'>Integrating Yahoo! BOSS with your ruby on rails application</a> <small>What is Yahoo! BOSS? Yahoo developer website cites it as:...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/08/18/source-code-for-ajax-based-drag-drop-navigation-tree-in-ruby-on-rails-the-tree-works-well-with-firefox-and-ie-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fully Ajax Based Drag Drop Navigation Tree For Ruby on Rails</title>
		<link>http://vinsol.com/blog/2006/08/09/fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails/</link>
		<comments>http://vinsol.com/blog/2006/08/09/fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails/#comments</comments>
		<pubDate>Wed, 09 Aug 2006 05:10:27 +0000</pubDate>
		<dc:creator>Sur Max</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax tree]]></category>
		<category><![CDATA[drag drop tree]]></category>
		<category><![CDATA[navigation tree]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">https://ajaxonrails.wordpress.com/2006/08/09/fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails/</guid>
		<description><![CDATA[Ajaxified tree comes with the following features
1.) Containing the ajax links for the tree items using link_to_remote
2.) Highlighting the selected node of the tree and other custom effects.
3.) Two way syncronization between the tree nodes and the currently displayed item on the web-page.
4.) Supports the tree architecture provided by the acts_as_tree.
5.) Clear and self traversing [...]


Related posts:<ol><li><a href='http://vinsol.com/blog/2010/03/29/jquery-full-calendar-with-rails/' rel='bookmark' title='Permanent Link: Jquery Full Calendar with Ruby on Rails'>Jquery Full Calendar with Ruby on Rails</a> <small>Contrary to popular belief, working on a client project gives...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Ajaxified tree comes with the following features</p>
<p>1.) Containing the ajax links for the tree items using link_to_remote</p>
<p>2.) Highlighting the selected node of the tree and other custom effects.</p>
<p>3.) Two way syncronization between the tree nodes and the currently displayed item on the web-page.</p>
<p>4.) Supports the tree architecture provided by the acts_as_tree.</p>
<p>5.) Clear and self traversing upto the selected node and all remaining nodes remained closed.</p>
<p>6.) Drag-n-Drop  sorting of the tree nodes, including the functionality of even changing the parent of the node.</p>
<p>This tree works very fine in my application and hope it will help u also. Check out the <a href="http://ajaxonrails.wordpress.com/2006/08/18/sorce-code-for-ajax-based-drag-drop-navigation-tree-for-rails/">Source Code</a> of the tree.</p>


<p>Related posts:<ol><li><a href='http://vinsol.com/blog/2010/03/29/jquery-full-calendar-with-rails/' rel='bookmark' title='Permanent Link: Jquery Full Calendar with Ruby on Rails'>Jquery Full Calendar with Ruby on Rails</a> <small>Contrary to popular belief, working on a client project gives...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/08/09/fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ajax development</title>
		<link>http://vinsol.com/blog/2005/12/10/ajax/</link>
		<comments>http://vinsol.com/blog/2005/12/10/ajax/#comments</comments>
		<pubDate>Sat, 10 Dec 2005 12:31:22 +0000</pubDate>
		<dc:creator>mj</dc:creator>
				<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://ajax4u.com/sample-post/</guid>
		<description><![CDATA[We have been doing a lot of ajax development recently.
We have been developing ajax applications with xajax, the ajax library in php. Also we have been using ruby on rails, with the scriptaculous and prototype library.


No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>We have been doing a lot of ajax development recently.</p>
<p>We have been developing ajax applications with xajax, the ajax library in php. Also we have been using ruby on rails, with the scriptaculous and prototype library.</p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2005/12/10/ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
