<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Coding With Ruby on Rails</title>
	<atom:link href="http://justinbgood.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://justinbgood.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Mon, 14 Nov 2011 20:11:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='justinbgood.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Coding With Ruby on Rails</title>
		<link>http://justinbgood.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://justinbgood.wordpress.com/osd.xml" title="Coding With Ruby on Rails" />
	<atom:link rel='hub' href='http://justinbgood.wordpress.com/?pushpress=hub'/>
		<item>
		<title>AJAX with Ruby on Rails! Connecting to the Database!</title>
		<link>http://justinbgood.wordpress.com/2009/07/16/ajax-with-ruby-on-rails-connecting-to-the-database/</link>
		<comments>http://justinbgood.wordpress.com/2009/07/16/ajax-with-ruby-on-rails-connecting-to-the-database/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 02:16:41 +0000</pubDate>
		<dc:creator>justinbgood</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[AJAX Search]]></category>
		<category><![CDATA[Asynchronous]]></category>
		<category><![CDATA[Buildings]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Process]]></category>
		<category><![CDATA[Query]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Request]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://justinbgood.wordpress.com/?p=44</guid>
		<description><![CDATA[IF you are looking for good tips on how to make proper AJAX calls in Ruby on Rails, this post is for YOU! I had to look around for the longest time to find good RoR examples of using AJAX, and yet, there really wasn&#8217;t a lot out there. However, once I finally figured out [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=44&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>IF you are looking for good tips on how to make proper AJAX calls in Ruby on Rails, this post is for YOU! I had to look around for the longest time to find good RoR examples of using AJAX, and yet, there really wasn&#8217;t a lot out there. However, once I finally figured out I realized how EASY Ruby on Rails makes using Javascript and AJAX calls. So now, I have come here to show YOU what took me soo long to figure out.</p>
<p>First off lets jump to our end goal. What are we trying to achieve? Let&#8217;s say we are creating an application and on one of our pages we want to gather information from our database without having to make the user redirect to another page or refresh the current one. This is exactly what AJAX was built for. Asynchronous calls between the server and the database.</p>
<p>In this example, we are going to implement a &#8220;building&#8221; search bar that will search our database for Building objects by building name. I have found this useful if you are trying to use google maps API and create a map with your own database stored buildings.</p>
<p>First things first&#8230; newer versions of Ruby on Rails such as 2.0 and above already have an awesome Javascript/AJAX library included: Prototype! This library is going to make our lives a LOT easier, however, I will also show you how to do these AJAX calls WITHOUT using Prototype so you see REALLY whats going on.</p>
<p>Open up your layout/application.html.erb or site.html.erb (yours could be named otherwise, but basically whatever layout you are using). Mine looks something like this:</p>
<p><pre class="brush: plain;">

&amp;lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;&amp;lt;%= @title %&amp;gt;&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;div id=&quot;container&quot;&amp;gt;
&amp;lt;div id=&quot;site_content&quot;&amp;gt;
&amp;lt;%= yield %&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

</pre></p>
<p>We need to first allow our application to use the Prototype library by adding this to our &lt;head&gt; section:</p>
<p><pre class="brush: plain;">

&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;&amp;lt;%= @title %&amp;gt;&amp;lt;/title&amp;gt;
&amp;lt;%= javascript_include_tag :defaults %&amp;gt;
&amp;lt;/head&amp;gt;

</pre></p>
<p>This will allow us to use all of the Prototype library calls. Next we need to create our Javascript function that will be called when the user hits our search button. (note: We could also use Prototype to have an AJAX called be sent on a time interval without having the user to click anything. This is similar to what facebook uses for notifying users of new notifications and messages.) I am going to call my function ajaxSearch(). You are going to also want to put your script in the layout header as well, so it is accessible on all pages.</p>
<p><pre class="brush: plain;">

&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;&amp;lt;%= @title %&amp;gt;&amp;lt;/title&amp;gt;
&amp;lt;%= javascript_include_tag :defaults %&amp;gt;

&amp;lt;script&amp;gt;

function ajaxSearch() {
//this is where our search code will go

}; // end ajaxSearch function

&amp;lt;/script&amp;gt; // end script
&amp;lt;/head&amp;gt;

</pre></p>
<p>Now lets make our search bar and button so that we can call our ajaxSearch function, even though it is not filled with code yet. This code can be placed anywhere on the app. If you want it on every page you need to put it in the layout page, but if you want it on a single page, just put it on that one. Either decision you choose your code is going to look like something like this.</p>
<p><pre class="brush: plain;">

&amp;lt;div id=&quot;search_box&quot;&amp;gt;
&amp;lt;form action=&quot;#&quot; onsubmit=&quot;ajaxSearch(); return false;&quot;&amp;gt; // this sets ajaxSearch() to be called form submit
&amp;lt;input style=&quot;width: 300px;&quot; type=&quot;text&quot; name=&quot;q&quot; value=&quot;&quot; id=&quot;q&quot; /&amp;gt;
&amp;lt;input id=&quot;search&quot; type=&quot;submit&quot; value=&quot;Search&quot; /&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;

</pre></p>
<p>Okay. So now we have a text bar that we can write into and calls the ajaxSearch() function. Now we need to handle the call and do our AJAX search. Change your ajaxSearch to look like this.</p>
<p><pre class="brush: plain;">

function ajaxSearch() {
var query = document.getElementById('q').value;
if( query == '' ) { return; }

document.getElementById(&quot;search&quot;).value = 'Searching...';
document.getElementById(&quot;search&quot;).disabled = true;

// AJAX call will go here
}; // end ajaxSearch function

</pre></p>
<p>What we have done is saved the text in the box to query, returning if it is null, and changed our search bar to be unaccessible while the AJAX search will be working. Next we need to create our AJAX request. This is where Prototype is going to become our best buddy! Usually when you make a AJAX XMLHttpRequest you need to check for browser compatibility and do lots of other LONG code-y stuff that is good to know but quite annoying. Prototype library AJAX request handles all of this for you behind the scenes, and it comes with Rails! Now lets enter our AJAX request where the // AJAX call will go here &#8211; was:</p>
<p><pre class="brush: plain;">
req = new Ajax.Request('/buildings/ajax_buildings', {
method: 'get',
parameters: { q: query },
onComplete: function(transport) {
processReqChange(transport);
}
});

</pre></p>
<p>beautiful, isn&#8217;t it? What this does is sends an AJAX request to our Buildings controller and calls the &#8216;ajax_buildings&#8217; action as a GET-er, while also sending the parameters params[:q], which is our query object, and on request completion will run processReqChange(), another function we must write. Lets take a look at our controller code, which in this example is for returning and array of Building objects. This part looks really sweet too.</p>
<p><pre class="brush: plain;"> // buildings/ajax_buildings &amp;lt;-- in buildings_controller.rb

def ajax_buildings
if params[:q]
@buildings = Building.find( :all,
:conditions =&amp;gt; [ 'name LIKE ?', '%' + params[:q].downcase + '%'],
:limit =&amp;gt; 20)

render :json =&amp;gt; @buildings
end
end

</pre></p>
<p>This action searches our application database for all Building objects that have their name property containing the suggested word entered in the search bar &#8211; params[:q], and I just threw a limit of 20 on their to stop possible over-flooding of objects. We then return a rendered text, an array of buildings, in a json format. That is JavaScript Object Notation. Although JavaScript can not directly use this string, we can change it into its proper format in a second.</p>
<p>Now for the final piece of our puzzle, we need to create our processReqChange() function that is called once the AJAX search is finished.</p>
<p><pre class="brush: plain;"> // inside of our &amp;lt;script&amp;gt;

function processReqChange(req) {
// request is complete
if (req.readyState == 4) {
var results = req.responseText;
var buildings = eval(&quot;(&quot;+results+&quot;)&quot;);
var size = buildings.length;

if(buildings.length == 0) {
alert(&quot;No matches were found!&quot;);
return;
}

// do all the work you want with your data

// CODE

// CODE

//  CODE

} // end if req == 4

}; // end function

</pre></p>
<p>Notice that what we sent in as &#8216;transport&#8217; is now called &#8216;req&#8217;. First we double check to make sure the readyState is equal to 4, which means the AJAX request is completely finished and we may now do our work with the data. We then store the response text into &#8216;results&#8217; (this is our array of buildings string in json format). The next line of code: var buildings = eval(&#8220;(&#8220;+results+&#8221;)&#8221;); &#8211; is a special command that I was told by my mentor is usually never used except for special circumstances, such as this! This essentially turns that string of a bulding array into an ACTUAL array that we can use! That&#8217;s right, we can USE it nowlike ANY other JavaScript array. buildings[0] will yield the first building in the array and you can use call ALL of the parameters of the building as such. Ex: buildings[0].name &#8211; will yield the first building&#8217;s name in the array.</p>
<p>Now that you have an array of the data you wanted, you can fill in the rest by manipulating and using that data however you want to. AWESOME! So, in conclusion, what we have done is easily and code-efficiently created a search bar that could potentially search and return any information, array of data or object from our database without having the user load or refresh to another page! It&#8217;s THAT easy!</p>
<p>Below is what your html.erb (or .rhtml) file would look liek put all together&#8230;</p>
<p><pre class="brush: plain;">

&amp;lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;&amp;lt;%= @title %&amp;gt;&amp;lt;/title&amp;gt;

&amp;lt;%= javascript_include_tag :defaults %&amp;gt;

&amp;lt;script&amp;gt;

function ajaxSearch() {
var query = document.getElementById('q').value;
if( query == '' ) { return; }

document.getElementById(&quot;search&quot;).value = 'Searching...';
document.getElementById(&quot;search&quot;).disabled = true;

req = new Ajax.Request('/buildings/ajax_buildings', {
method: 'get',
parameters: { q: query },
onComplete: function(transport) {
processReqChange(transport);
}

}); // end ajax req
}; // end ajaxSearch function

function processReqChange(req) {
// request is complete
if (req.readyState == 4) {
var results = req.responseText;
var buildings = eval(&quot;(&quot;+results+&quot;)&quot;);
var size = buildings.length;

if(buildings.length == 0) {
alert(&quot;No matches were found!&quot;);
return;
}

// do all the work you want with your data

// CODE

// CODE

//  CODE

} // end if req == 4

}; // end function

&amp;lt;/script&amp;gt;

&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;div id=&quot;container&quot;&amp;gt;
&amp;lt;div id=&quot;site_content&quot;&amp;gt;

&amp;lt;div id=&quot;search_box&quot;&amp;gt;
&amp;lt;form action=&quot;#&quot; onsubmit=&quot;ajaxSearch(); return false;&quot;&amp;gt; // this sets ajaxSearch() to be called form submit
&amp;lt;input style=&quot;width: 300px;&quot; type=&quot;text&quot; name=&quot;q&quot; value=&quot;&quot; id=&quot;q&quot; /&amp;gt;
&amp;lt;input id=&quot;search&quot; type=&quot;submit&quot; value=&quot;Search&quot; /&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

</pre></p>
<p>That, and putting in the correct Controller Action &#8211; ajax_buildings (or w/e your desired object is) and your set! This can be used for a lot of different things, such as calling objects, having a text search engine for your own site, uploading notifications instantly, basically anything you can imagine. It&#8217;s good to know that something as difficult as AJAX calls and JS functions can be so simple and straight forward! Thank you so much for reading my article on AJAX on Ruby on Rails. If you like it please feel free to leave a comment. You can also e-mail me if you have any questions at jbryant@inigral.com.</p>
<p>More information on the Prototype library can be found at their website: http://www.prototypejs.org/learn</p>
<p>If you liked this article, please link it on yours or any other site! Thanks a bunch guys! Good coding to all!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justinbgood.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justinbgood.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justinbgood.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justinbgood.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justinbgood.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justinbgood.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justinbgood.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justinbgood.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justinbgood.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justinbgood.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justinbgood.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justinbgood.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justinbgood.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justinbgood.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=44&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justinbgood.wordpress.com/2009/07/16/ajax-with-ruby-on-rails-connecting-to-the-database/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43206c086c628fff708a5de2c0ba31b7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">justinbgood</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby on Rails &#8211; YouTube on Facebook</title>
		<link>http://justinbgood.wordpress.com/2008/09/17/ruby-on-rails-youtube-on-facebook/</link>
		<comments>http://justinbgood.wordpress.com/2008/09/17/ruby-on-rails-youtube-on-facebook/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 00:58:48 +0000</pubDate>
		<dc:creator>justinbgood</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://justinbgood.wordpress.com/?p=38</guid>
		<description><![CDATA[So for one of my websites, I wanted to allow users the ability to post youtube videos of themselves for there skateboarding. Now after thinking it over I thought of a pretty primitive yet effective way to quickly, and efficiently do it without taking up lots of space or memory. Its as easy as this. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=38&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So for one of my websites, I wanted to allow users the ability to post youtube videos of themselves for there skateboarding. Now after thinking it over I thought of a pretty primitive yet effective way to quickly, and efficiently do it without taking up lots of space or memory.</p>
<p>Its as easy as this. Create a column you want int the correct table called &#8220;video&#8221;. Example: If you want each User to have their own video you would add a column, you would use a migration file to add a text column to the User table. That way you could now do User.video and it would point to a text object.</p>
<p>Now, for Facebook to properly use a you tbe video you must call the FBML function:</p>
<p><code>&lt;fb:swf swfsrc="" width="x" height="y" /&gt;</code></p>
<p>Now, the link to the video must be a certain format, and it is NOT the default hyperlink to the actual video. Say you were to search for a video it would look something like this. </p>
<p><code>http://www.youtube.com/watch?v=cbYrXR87cvE</code></p>
<p>However, if you put this in as the hyperlink it will NOT work. The correct link that it requires looks a little different. Instead of /watch?v=[video ID] it looks for a /v/[video ID]. Thus the link would have to be http://www.youtube.com/v/cbYrXR87cvE. So, I figure the easiest way to make this for the user is, allow them to use the INCORRECT link to store into your databse. But, inside your Model Helper create this helper function. Call it youtube_embed.</p>
<p><code>module CrewsHelper<br />
  def youtube_embed(video_id)<br />
    return video_id.gsub('watch?v=', 'v/')<br />
  end<br />
end</code></p>
<p>What this does is recieves a String video_id (coul dbe named anything) and uses the usefule ruby gsub method to simply replace the &#8220;watch?v&#8221; with &#8220;v/&#8221;. This will turn it into the correct hyperlink format to work. Now to actually you the method you would do something like this.</p>
<p><code><br />
 # if the video param for User is not null (i.e. has a link) do this<br />
        &lt;fb:swf swfsrc="" width="460" height=425" /&gt;</p>
<p></code></p>
<p>Now, every time that page is loaded, if the user has a youtube video that he put into his video param, it will show up on that page! Pretty straight forward I hope. Enjoy!</p>
<p>-Justin Bryant</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/justinbgood.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/justinbgood.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justinbgood.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justinbgood.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justinbgood.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justinbgood.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justinbgood.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justinbgood.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justinbgood.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justinbgood.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justinbgood.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justinbgood.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justinbgood.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justinbgood.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justinbgood.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justinbgood.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=38&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justinbgood.wordpress.com/2008/09/17/ruby-on-rails-youtube-on-facebook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43206c086c628fff708a5de2c0ba31b7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">justinbgood</media:title>
		</media:content>
	</item>
		<item>
		<title>AB Email Testing</title>
		<link>http://justinbgood.wordpress.com/2008/08/18/ab-email-testing/</link>
		<comments>http://justinbgood.wordpress.com/2008/08/18/ab-email-testing/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 23:54:45 +0000</pubDate>
		<dc:creator>justinbgood</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AB Email Testing]]></category>
		<category><![CDATA[Ab Testing]]></category>
		<category><![CDATA[ActionMailer]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Email Marketing]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://justinbgood.wordpress.com/?p=34</guid>
		<description><![CDATA[Today I learned some nifty tricks for AB email testing for ruby on rails applications. What is AB Email Testing you ask? Thats a good place to start at. When you own an application that has many users you are going to want to email them information. Whether its to tell them of a new [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=34&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I learned some nifty tricks for AB email testing for ruby on rails applications. What is AB Email Testing you ask? Thats a good place to start at. When you own an application that has many users you are going to want to email them information. Whether its to tell them of a new offer, or update them on something, your ultimate goal is to get that reader engaged in your email and follow the links you offer them. AB testing is a simple test of sending out multiple versions on an email to all your users and seeing which version gets the highest amount of users following the link you supplied them. This is a very important test for marketing your emails. You want to use the email with the greatest success rate. So now we have a few things we need to do to get the to work. 1) we need a easy way to send emails to all your users. 2) we need a system of sending different versions of that email to each user. 3) we need a way to store and keep track of how many users clicked each link and figure out which one has the higest success rate.</p>
<p>Hopefully after this blog entry you will be able to create your own AB test system. There are many programs and companies that will sell you this service, which would probably be much easier, but sometimes the easiest route isn&#8217;t the smartest. By following this blog you will be able to fully create and control your own AB email tesing system, making it as specific and customized for your app as possible.</p>
<p>For this blog I am going to assume that you have an ActionMailer set up and working for your application. The ActionMailer I will be referring to in my blog is called SignupMailer. Yours may be called anything as long as it inherits the ActionMailer class. Now lets begin&#8230;</p>
<p>The first thing we are going to want to do is create a table in our database that will store all the information we desire. Lets create a model for our tracking system&#8230; I called my model &#8220;Click&#8221;. Everytime a user hits the link button a Click will be created and added to the database, and that will hold all the information we will find useful. Lets start this now shall we?</p>
<p>go ahead and -&gt; ruby script/generate model Click</p>
<p>now go into your newly created migration file called. ####_create_clicks. Lets add some useful information that it shoul dbe holding. For mine I woul dlike to keep track of a few things. 1) The User who clicked the link. 2) The name of the email being sent. 3) what version of the email it is. 4) the location you want to send the user too after they click the link. Here is my migration file<br />
<code><br />
class CreateClicks &lt; ActiveRecord::Migration<br />
  def self.up<br />
    create_table :clicks do |t|<br />
      t.column  :user_id,     :integer # user id<br />
      t.column  :email_name,  :string #email name<br />
      t.column  :variation,   :integer #version number<br />
      t.column  :link,        :string  # link we want to send our users to<br />
      t.column  :created_at,  :timestamp # information on when the link was pressed<br />
    end<br />
  end</p>
<p>  def self.down<br />
    drop_table :clicks<br />
  end<br />
end<br />
</code></p>
<p>now you can rake db:migrate the newly made migration. So now we got our table which we can create Clicks and add them to the database. How and when do we create these clicks? and how do we send the emails? There are many different ways to go about doing this, but I will do what works well for me. I am going to create a rake file that by simply executing from the command line will send all of your users emails of different versions with the ability to track whether or not they have been clicked.<br />
The next thing we need to do is create a controller for our Clicks. Go ahead and<br />
<code>ruby script/generate controller Clicks</code><br />
inside make a function <em>link</em>.<br />
<code>def link<br />
end<br />
</code><br />
we will fill that in later. Now lets begin with our rake file. Move into your lib/tasks folder and create our rakefile.  If you do not have this directory simply create one. Now lets assume we have a new promotional email we want to send out and we want to find out which of the 3 versions we have is most efficient at drawing users. We will want to create a rakefill called promo1 (assuming you may have other promos later we will want to start with 1 and work our way up after that. so create your promo1.rake file inside lib/tasks and open it up. inside of it put this.<br />
<code><br />
# Provide tasks to load and delete user data<br />
require 'active_record'<br />
require 'active_record/fixtures'</p>
<p>namespace :db do<br />
  namespace :promo1 do </p>
<p>    desc "Send The Emails!"<br />
    task :load =&gt; :environment do |t|<br />
      send_emails<br />
    end<br />
  end<br />
end</p>
<p>def send_emails<br />
end<br />
</code><br />
when you run this rake file it will load and run the send_email functions which we will now create.</p>
<p><code><br />
def send_emails<br />
  users = User.find :all # gets all users in database</p>
<p>  users.each { |user| # goes through each user</p>
<p>    variation = rand(2)+1 # picks a random number from 1 - 3<br />
    email_name = "promo_" + variation.to_s # our promo name with the variation at the end<br />
    redirect_control = "login" # the controller we want to call<br />
    redirect_action = "login" # the action we want to call from that controller</p>
<p># now we will check which of the 3 versions we will send to the user<br />
    if variation == 1<br />
#we must send a bunch of these params to the ActionMailer so it can use it later<br />
      SignupMailer::deliver_promo_1(user, email_name, variation, redirect_control, redirect_action)</p>
<p>    elsif variation == 2<br />
      SignupMailer::deliver_promo_2(user, email_name, variation, redirect_control, redirect_action)</p>
<p>    elsif variation == 3<br />
      SignupMailer::deliver_promo_3(user, email_name, variation, redirect_control, redirect_action)</p>
<p>    end<br />
  }</p>
<p>end<br />
</code><br />
and thats it. the rakefile is done. But we are not quite done yet. Lets now go back to our clicks controller. Now lets fill in what we want in the link function. </p>
<p><code><br />
def link<br />
    # all of these params we will specify in our link we create for our emails<br />
    user_id = params[:id]<br />
    email_name = params[:email]<br />
    variation = params[:variation]<br />
    control = params[:redirect_control]<br />
    action = params[:redirect_action]</p>
<p>    link = "http://www.yoururl.com/#{control}/#{action}"</p>
<p>    click = Click.new(:user_id =&gt; user_id,<br />
                      :email_name =&gt; email_name,<br />
                      :variation =&gt; variation,<br />
                      :link =&gt; link,<br />
                      :created_at =&gt; DateTime.now)</p>
<p>    click.save</p>
<p>    redirect_to :controller =&gt; control, :action =&gt; action<br />
  end<br />
</code></p>
<p>pretty straight forward huh? we just send it all the info we desire from the email. Save it into a new click object. and voila! its stored in our database. Now lets create our promo_(1-3) emails. Lets open our ActionMailer model and create the proper function calls.</p>
<p>SignupMailer.rb (model file)<br />
<code><br />
def promo_1(user, email_name, variation, redirect_control, redirect_action)<br />
    from "admin@standardissimo.com"<br />
    recipients user.email<br />
    subject email_name<br />
    content_type "text/html"<br />
    body( :recipient =&gt; user,<br />
          :email_name =&gt; email_name,<br />
          :variation =&gt; variation,<br />
          :redirect_control =&gt; redirect_control,<br />
          :redirect_action =&gt; redirect_action)<br />
  end</p>
<p>  def promo_2(user, email_name, variation, redirect_control, redirect_action)<br />
    from "admin@standardissimo.com"<br />
    recipients user.email<br />
    subject email_name<br />
    content_type "text/html"<br />
    body( :recipient =&gt; user,<br />
          :email_name =&gt; email_name,<br />
          :variation =&gt; variation,<br />
          :redirect_control =&gt; redirect_control,<br />
          :redirect_action =&gt; redirect_action)<br />
  end</p>
<p>  def promo_3(user, email_name, variation, redirect_control, redirect_action)<br />
    from "admin@standardissimo.com"<br />
    recipients user.email<br />
    subject email_name<br />
    content_type "text/html"<br />
    body( :recipient =&gt; user,<br />
          :email_name =&gt; email_name,<br />
          :variation =&gt; variation,<br />
          :redirect_control =&gt; redirect_control,<br />
          :redirect_action =&gt; redirect_action)<br />
  end<br />
</code><br />
so here we are sending all of our given params to the BODY which is very important. The body is what gets sent to the email .rhtml file to be used as a local variable. Now lets create our new SignupMailer .rhtml email files. Move into your views/SignupMailer folder and create promo_1 &#8211; promo_3. Now lets add the actual code to this .rhtml file.</p>
<p>views/signup_mailer/promo_1.rhtml<br />
<code></p>
<h2> Promo 1 </h2>
<p>&lt;%="<a href='http://www.yoururl.com/clicks/link/#{@recipient.id}?email=#{@email_name}&amp;variation=#{@variation}&amp;redirect_control=#{@redirect_control}&amp;redirect_action=#{@redirect_action}'>" %&gt;Click here four our promo!</a></p>
<p>now here is the true beauty of all this. we are actually sending our users who click on this link directly to our click controllers link action while sending with it the necessary parms such as user_id, email, variation, control and action. Once you copy this for your other 2 promos, changing the wording and varying the way it feels you are DONE. thats right. ready to test now? all you have to do for this to work is go to your console, move into your application and run<br />
</code>rake db:promo1:load<code><br />
and shabam! you should have a bunch of emails all of different random variations being sent out, with the capability of tracking which emails were clicked with detailed information about them including when exactly it was clicked. Thanks for reading my tutorial and I hoped this helped some AB email testers out there. I do realize that there are probably other ways to go about doing this. I have found that this is the simplest and efficient way to create your own working AB email testing system. Thanks all!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/justinbgood.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/justinbgood.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justinbgood.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justinbgood.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justinbgood.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justinbgood.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justinbgood.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justinbgood.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justinbgood.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justinbgood.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justinbgood.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justinbgood.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justinbgood.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justinbgood.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justinbgood.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justinbgood.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=34&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justinbgood.wordpress.com/2008/08/18/ab-email-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43206c086c628fff708a5de2c0ba31b7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">justinbgood</media:title>
		</media:content>
	</item>
		<item>
		<title>Rake Files To Upload Test Users!!!</title>
		<link>http://justinbgood.wordpress.com/2008/08/04/29/</link>
		<comments>http://justinbgood.wordpress.com/2008/08/04/29/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 23:22:41 +0000</pubDate>
		<dc:creator>justinbgood</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[rake files]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://justinbgood.wordpress.com/?p=29</guid>
		<description><![CDATA[Today I learned a few tips and trick about uploading data into a database for testing purposes. Lets say you are creating a large social network application that will have many users and many associations between those users. When you are in the development stage you may want to see exactly how your code will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=29&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I learned a few tips and trick about uploading data into a database for testing purposes. Lets say you are creating a large social network application that will have many users and many associations between those users. When you are in the development stage you may want to see exactly how your code will work when you have many users. A great way to do this is upload a large amounts of test data or users to &#8220;test&#8221; the functionality of your site.</p>
<p>Although there are many ways to do this, I have found that a good way to accomplish this is by using rake files.  You can call a rake task when you are in your console and it creates information quickly and efficiently. I need to state that the source code for this was taken from the Insoshi open source code and then modified. In my example I will create many users, give them jobs and then put them all into one organization. This way, when I start the site I can check and see if everything is working ok.</p>
<p>The first thing you will need to do is create a tasks folder inside your lib folder (if it is not already created). Inside your tasks folder you will need to make a test_data.rake file, a install.rake file, and a test_data folder. Move into your test_data folder. Now create a female_names.txt and a male_names.txt. Inside these files type in the names of the example users you will want to create. example:</p>
<p>male_names.txt<br />
<code><br />
Jimmy Dangle<br />
Fredrick Yammington<br />
Robert Coolridge<br />
Michael Monty<br />
William Wanker<br />
David Devreux<br />
Richard Rottingham<br />
Charles Day<br />
Joseph Myles<br />
Thomas Keaton<br />
Christopher Wallace<br />
Daniel Duckard<br />
</code><br />
do something similar for your females. Now. Move back into the tasks folder and open up install.rake. Input this into the file.</p>
<p>install.rake<br />
<code><br />
# Provide tasks to load and delete test user data.<br />
require 'active_record'<br />
require 'active_record/fixtures'</p>
<p>desc "Install AppName" # change Appname with your Application Name<br />
task :install =&gt; :environment do |t|<br />
  Rake::Task["db:migrate"].invoke<br />
end<br />
</code></p>
<p>Now open test_data.rake. you will want to start your file by giving it the proper ActiveRecord inputs like so.</p>
<p>test_data.rake<br />
<code><br />
# Provide tasks to load and delete test user data.<br />
require 'active_record'<br />
require 'active_record/fixtures'</p>
<p>DATA_DIRECTORY = File.join(RAILS_ROOT, "lib", "tasks", "test_data")<br />
</code></p>
<p>Now we will create the code that will run when it is called.</p>
<p><code><br />
namespace :db do<br />
  namespace :test_data do </p>
<p>    desc "Load test data"<br />
    task :load =&gt; :environment do |t|<br />
      # this will call a method to create people. We will make this in a second.<br />
      create_people<br />
      # you may call any other function you desire here<br />
    end</p>
<p>    # destroy the databse<br />
    desc "Remove test data"<br />
    task :remove =&gt; :environment do |t|<br />
      Rake::Task["db:migrate:reset"].invoke<br />
      # get rid of anything you may not want to accumulate<br />
      # Blow away the Ferret index.<br />
      # system("rm -rf index/")<br />
      # Remove images to avoid accumulation.<br />
      # system("rm -rf public/photos")<br />
    end</p>
<p>    # reloads the test data<br />
    desc "Reload test data"<br />
    task :reload =&gt; :environment do |t|<br />
      Rake::Task["db:test_data:remove"].invoke<br />
      Rake::Task["install"].invoke<br />
      Rake::Task["db:test_data:load"].invoke<br />
    end<br />
  end<br />
end<br />
</code></p>
<p>Okay. Now we move to the next step. Creating the Create People function. Below the code you just make above put in this.</p>
<p>test_data.rak<br />
<code><br />
...<br />
end</p>
<p>def create_people<br />
  %w[male female].each do |gender| # goes through both male and female users<br />
    filename = File.join(DATA_DIRECTORY, "#{gender}_names.txt")<br />
    names = File.open(filename).readlines<br />
    password = "foobar"</p>
<p>    names.each_with_index do |name, i| # reads through each name in the .txt file<br />
      name.strip! # strips the name of any trailing spaces. </p>
<p>      # Now here's the trick. I want to make Users because my app uses User Models.<br />
      # You will have to change user to w/e ur app uses.<br />
      # example. person = Person.create! (if you uses Person)<br />
      user = User.create!(    :email =&gt; "#{name.split[0].downcase}@example.com",<br />
                              :password =&gt; password,<br />
                              :name =&gt; name,<br />
                              :user_type =&gt; "Instructor") # my users have a user type. Yours may not</p>
<p>      # so now I've created a bunch of users. Voila. They are in the database<br />
      # however I also want to test some other things I have for my users<br />
      # this code will NOT apply to your application but will give you some<br />
      # insight into how to create your own user associations....</p>
<p>      # create the jobs for each person<br />
      # for the purpose of testing<br />
      job = Job.create!(  :title =&gt; "Teacher",<br />
                          :user_id =&gt; user.id,<br />
                          :school_id =&gt; 8,<br />
                          :district_id =&gt; 3,<br />
                          :state_id =&gt; 44,<br />
                          :is_primary =&gt; true  )</p>
<p>      user.jobs &lt; "Fight For Better Wages",<br />
                            :public =&gt; true,<br />
                            :group_type =&gt; "Protest",<br />
                            :district_id =&gt; 3,<br />
                            :author_id =&gt; user.id,<br />
                            :state_id =&gt; 44,<br />
                            :school_id =&gt; 8 )<br />
        user.groups &lt;&lt; g # gives the author user the group as well<br />
      # add all others to group<br />
      else<br />
        # find the group just created and put them into it<br />
        g = Group.find_by_name "Fight For Better Wages"<br />
        user.groups &lt;&lt; g<br />
      end</p>
<p>    end<br />
  end<br />
end<br />
</code></p>
<p>There you go. Now lets RUN it. Go to your console and change to your application folder. In my example it would be ( cd dev/standardissimo). Now run the rake file. </p>
<p><code>rake db:test_data:reload</code></p>
<p>After this runs all the users and appropriate associations should have been created. Now for all the troubles I ran into. The program i made to use this was running of rails 1.2.4 and it gave me an error in my test_data.rake file with Rake::Task["db:migrate:reset"].invoke so I had to comment it out. This means that after I was done looking at the test users and wanted to get rid of them I had to either A) drop the database and start fresh or B) drop the Users, tables and all other associated tables. I decided to go with route B just because my rake db:migrate takes a LONG time. While I was doing this I noticed that when I tried to drop my Users tables (in MySQL -&gt; delete from users;) it came up with an error.<br />
<code>Cannot delete or update a parent row: a foreign key constraint fails (`standardissimo/groups`, CONSTRAINT `fk_groups_author_id` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`))</code><br />
What this mean was that there was an association still linked that needed to be deleted. To fix this i had to delete jobs first (MySQL -&gt; delete from jobs;) and then delete the users. Now when I made the groups I had the same problem. Except i got that error when i tried to even delete the groups table. I was quite confused about this until I realized that i had a groups_users table that existed and worked as an association link between the user and groups tables. So I had to delete the groups_users table first, then delete the groups and finally users table. Now this is a pain in the ass so I reccommend upgrading to rails 2.1. That way your rake db:migrate:reset will do that all for you. Anyways&#8230; i think that about wraps it up. Hope this helps a little bit!</p>
<p>-Justin Brant</p>
<p>Below is my entire test_data.rake file<br />
<code><br />
# Provide tasks to load and delete sample user data.<br />
require 'active_record'<br />
require 'active_record/fixtures'</p>
<p>DATA_DIRECTORY = File.join(RAILS_ROOT, "lib", "tasks", "test_data")</p>
<p>namespace :db do<br />
  namespace :test_data do </p>
<p>    desc "Load sample data"<br />
    task :load =&gt; :environment do |t|<br />
      @lipsum = File.open(File.join(DATA_DIRECTORY, "lipsum.txt")).read<br />
      create_people<br />
      #make_connections<br />
      #make_messages(@lipsum)<br />
      #make_forum_posts<br />
      #make_blog_posts<br />
      #make_feed<br />
    end</p>
<p>    desc "Remove sample data"<br />
    task :remove =&gt; :environment do |t|<br />
      #Rake::Task["db:migrate:reset"].invoke<br />
      # Blow away the Ferret index.<br />
      system("rm -rf index/")<br />
      # Remove images to avoid accumulation.<br />
      system("rm -rf public/photos")<br />
    end</p>
<p>    desc "Reload sample data"<br />
    task :reload =&gt; :environment do |t|<br />
      Rake::Task["db:test_data:remove"].invoke<br />
      Rake::Task["install"].invoke<br />
      Rake::Task["db:test_data:load"].invoke<br />
    end<br />
  end<br />
end</p>
<p>def create_people<br />
  %w[male female].each do |gender|<br />
    filename = File.join(DATA_DIRECTORY, "#{gender}_names.txt")<br />
    names = File.open(filename).readlines<br />
    password = "foobar"<br />
    photos = Dir.glob("lib/tasks/sample_data/#{gender}_photos/*.jpg").sort_by { rand }<br />
    last_names = @lipsum.split<br />
    names.each_with_index do |name, i|<br />
      name.strip!<br />
      #full_name = "#{name} #{last_names.pick.capitalize}"<br />
      user = User.create!(    :email =&gt; "#{name.split[0].downcase}@example.com",<br />
                              :password =&gt; password,<br />
                              #:password_confirmation =&gt; password,<br />
                              :name =&gt; name,<br />
                              :user_type =&gt; "Instructor")</p>
<p>      # Photo.create!(:uploaded_data =&gt; uploaded_file(photos[i], 'image/jpg'),<br />
      #             :person =&gt; person, :primary =&gt; true)</p>
<p>      # create the jobs for each person<br />
      job = Job.create!(  :title =&gt; "Teacher",<br />
                          :user_id =&gt; user.id,<br />
                          :school_id =&gt; 8,<br />
                          :district_id =&gt; 3,<br />
                          :state_id =&gt; 44,<br />
                          :is_primary =&gt; true  )</p>
<p>      user.jobs &lt; "Fight For Better Wages",<br />
                            :public =&gt; true,<br />
                            :group_type =&gt; "Protest",<br />
                            :district_id =&gt; 3,<br />
                            :author_id =&gt; user.id,<br />
                            :state_id =&gt; 44,<br />
                            :school_id =&gt; 8 )<br />
        user.groups &lt;&lt; g<br />
      # add all others to group<br />
      else<br />
        g = Group.find_by_name "Fight For Better Wages"<br />
        user.groups &lt;&lt; g<br />
      end</p>
<p>    end<br />
  end<br />
end<br />
</code></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/justinbgood.wordpress.com/29/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/justinbgood.wordpress.com/29/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justinbgood.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justinbgood.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justinbgood.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justinbgood.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justinbgood.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justinbgood.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justinbgood.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justinbgood.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justinbgood.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justinbgood.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justinbgood.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justinbgood.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justinbgood.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justinbgood.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=29&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justinbgood.wordpress.com/2008/08/04/29/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43206c086c628fff708a5de2c0ba31b7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">justinbgood</media:title>
		</media:content>
	</item>
		<item>
		<title>Ajax AutoComplete Help</title>
		<link>http://justinbgood.wordpress.com/2008/07/29/ajax-autocomplete-help/</link>
		<comments>http://justinbgood.wordpress.com/2008/07/29/ajax-autocomplete-help/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 22:36:20 +0000</pubDate>
		<dc:creator>justinbgood</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Auto Complete]]></category>
		<category><![CDATA[AutoComplete]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[search bar]]></category>

		<guid isPermaLink="false">http://justinbgood.wordpress.com/?p=22</guid>
		<description><![CDATA[Okay! So now you have a working database, and you want some way to have an autocomplete complete search bar for those database objects. How are we ever going to get this to work? Luckily for us both, there is the INTERNET! My blog will closely follow the code of another post, because it is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=22&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Okay! So now you have a working database, and you want some way to have an autocomplete complete search bar for those database objects. How are we ever going to get this to work? Luckily for us both, there is the INTERNET! My blog will closely follow the code of another post, because it is the code that I used as well. My job will be to clarify how it is used and some errors and troubles I ran into during my testing period of it.<br />
First, you will want to use the code from this website. <a href="http://wiki.developers.facebook.com/index.php/FBJS/Examples/Typeahead/AJAX">AJAX Auto Complete</a></p>
<p>This example works for facebook and can similarly be used for any other application or production. Firstly, the key to this is that the Options code AND the JavaScript code MUST be placed in SCRIPT braces inside the .html or .fbml file you want to have the search bar in. If you want it on every page you will want this code inside of your LAYOUT page, wherever you want it. I have my code in layout/_header.fbml.erb file, since I have mine at the top of the page in the header. ex:<br />
layout/_header.fbml.erb </p>
<p><code><br />
options = {<br />
	preMsgTxt: "search for topic...", // text to display when nothing has been typed<br />
	menuOpacity: 94, // opacity of the menu<br />
	ajaxUrl: _hostname+"/topics/auto_complete_for_table_thing", // url to your data source<br />
	focus: false, // whether or not to auto-focus the textbox upon creation<br />
	onEnter: function(value) { // handler for hitting the 'enter' key<br />
		 console.log(value);<br />
		 document.setLocation("http://apps.facebook.com/researchr/topics/search?topic_search="+value);<br />
	},<br />
	delayTime: 100, // amount of idle time after a keypress before making the ajax call<br />
	clearOnEnter: false // whether or not to clear the text after they hit enter<br />
}; </p>
<p>function ajaxSuggestFbml(obj, options) {<br />
	this.obj = obj; // Setup the events we're listening to<br />
	this.obj.addEventListener('focus', this.onfocus.bind(this))<br />
		.addEventListener('blur', this.onblur.bind(this))<br />
		.addEventListener('keyup', this.onkeyup.bind(this))<br />
		.addEventListener('keydown', this.onkeydown.bind(this))<br />
		.addEventListener('keypress', this.onkeypress.bind(this));<br />
}<br />
. . . . . . . . . blah blah blah ALLLL that code . . . . .<br />
</code></p>
<p>Okay now&#8230; phew. Here is the IMPORTANT facts! notice in your options the AjaxURL line. This will have to be changed to the directory of your auto_complete_for_table_thing function. Now, you will want your auto_complete_for_table_thing in the CONTROLLER of whatever it is your searching in your database. In my case, I want to search through my database for Research Topics that have title&#8217;s and body&#8217;s. So, I want to put my auto_complete_for_table_thing function inside my topics controller. here is my example:<br />
<code><br />
def auto_complete_for_table_thing # this is for the auto complete function in header.fbml.erb<br />
    topics = Topic.find(:all,<br />
                        :conditions =&gt; [ 'page_title LIKE ?', params[:suggest_typed].downcase + '%'],<br />
                        :limit =&gt; 30,<br />
                        :order =&gt; 'page_title ASC').map { |n| n.page_title }<br />
    topics = topics.uniq<br />
    topics = topics[0...10]<br />
    render :text =&gt; "{fortext:#{params[:suggest_typed].to_json},results:#{topics.to_json}}"<br />
  end<br />
</code><br />
notice the differences in my table to the suggested one. I made some changes (which you will have to make to) to make it work correctly for my usage. First I am searching through all of my topics objects in the database. I am using the :conditions =&gt; hash to tell it im looking for all instances where the topics &#8220;page_title&#8221; is LIKE ? (a MySQL command) the suggested type (what was entered inside the search bar). I chose page_title because that is intuitively what a user would be looking for. Example: if the user wanted to know something about Philosophy, most of my topics with have in its page_title Philosophy of &#8230;. Philosophy of &#8230; etc etc etc. That way, it will now be picked up during the AJAX call. I also had to uniq my topics because I had many topics with the same page_title as another one, this got rid of the ugly duplicates in the suggested topic results. This is not necessary unless you are getting multiple of the same objects you are looking for. </p>
<p>Okay, now so you created your auto_complete_for_table_thing function in your controller. You must make sure your AjaxUrl in your options is linked to it correctly like mine is.<br />
<code>ajaxUrl: _hostname+"/topics/auto_complete_for_table_thing", // url to your data source</code><br />
you may have to play with the link to get it pointed to your specific controller which contains the auto_complete function in it. Once its pointed correctly and you have inserted the neccessary JavaScript code given in the example, you will have to now create the actual search bar. There are many ways to create a search bar but I will show you my example. This also is in my layouts/_header.fbml.erb file. (this should be in the same file as the JavaScript and options.<br />
example:<br />
<code><br />
input id="search_box_topics" autocomplete="off" maxlength="20" type="text"/&gt;<br />
input type="button" value="search" onClick="suggestr.options.onEnter(suggestr.obj.getValue());"/&gt;<br />
var suggestr = new ajaxSuggestFbml(document.getElementById('search_box_topics'),options);<br />
</code><br />
this creates the search box and starts up your ajaxSuggestFbml java script function. Once this is put in KABLAM, it should work. If it doesn&#8217;t work you should check main things:<br />
1) your options and javascript is in the desired file<br />
2) your search box is in the same file as the java script<br />
3) your auto complete is in your controller and is set correctly<br />
4) your AjaxUrl is pointing correctly to the /controller/auto_complete_for_table_thing</p>
<p>with all of that correctly set up you should have a working auto complete search box connected to objects in your database!</p>
<p>Hope everything works out and that this was helpful!</p>
<p>P.S. Whatever you are searching for (be it first names, last names, titles) should be INDEXED in your migration. This will allow for faster database searching and will shave off a lot of seconds of &#8220;would be wait time&#8221; for users to get the search results. Google on indexing a column for MySQL databses, because that is what I used for mine.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/justinbgood.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/justinbgood.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justinbgood.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justinbgood.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justinbgood.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justinbgood.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justinbgood.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justinbgood.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justinbgood.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justinbgood.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justinbgood.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justinbgood.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justinbgood.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justinbgood.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justinbgood.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justinbgood.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=22&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justinbgood.wordpress.com/2008/07/29/ajax-autocomplete-help/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43206c086c628fff708a5de2c0ba31b7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">justinbgood</media:title>
		</media:content>
	</item>
		<item>
		<title>ActionMailer</title>
		<link>http://justinbgood.wordpress.com/2008/07/24/actionmailer/</link>
		<comments>http://justinbgood.wordpress.com/2008/07/24/actionmailer/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 20:29:59 +0000</pubDate>
		<dc:creator>justinbgood</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://justinbgood.wordpress.com/?p=15</guid>
		<description><![CDATA[The ability to control and use e-mail is a crucial part for most modern web applications. Whether you need to send an e-mail to retrieve a lost password or verify the authenticity of a users identity, Rails offers great support for sending and receiving e-mails with its ActionMailer and by allowing the ability to create [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=15&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The ability to control and use e-mail is a crucial part for most modern web applications. Whether you need to send an e-mail to retrieve a lost password or verify the authenticity of a users identity, Rails offers great support for sending and receiving e-mails with its ActionMailer and by allowing the ability to create mailer models. Rails by default tries to send e-mails via SMTP of localhost. If you have an SMTP daemon running and it accepts SMTP e-mails locally don&#8217;t have to do anything else to send e-mails. Otherwise you will need to decide another means for your system to send e-mails.<br />
<strong><br />
Mailer Models:</strong><br />
The way to create a mail model is like this -&gt; $ ruby script/generate mailer [mail name] ex: script/generate mailer LateNotice<br />
You work with ActionMailer classes by defining public mailer methods that correspond to the types of e-mails that you want to send.</p>
<p>And example of a mailer method:<code><br />
def late_timeshee(user, week_of)<br />
  recipients user.email<br />
  subject "[Time and Expenses] Late timesheet notice"<br />
  from "system@timeandexpenses.com"<br />
  body :recipient =&gt; user.name, :week =&gt; week_of<br />
end</code></p>
<p>Here is a list of all the mail-related options that you can set inside of mailer methods<br />
attachment: Specifies a file attachment. Can be called multiple times for multiple files<br />
<strong>bcc</strong>: Specifies blind recipient<br />
<strong>body</strong>: Defines body of the email. Can take a hash or a string. ActionMailer automatically normalizes lines<br />
<strong>cc</strong>: Specifies carbon copy recipients<br />
<strong>charset</strong>: The character set to use for message<br />
<strong>content_type</strong>: Specifies content type of the message. The default is text/plain<br />
<strong>from</strong>: Specifies the from address for the message<br />
<strong>headers</strong>: Additional headers to be added to the message hash<br />
<strong>implicit_parts_order</strong>: Array specifying the order in which the parts of a multipart email should be sorted base on their MIME content-type.<br />
<strong>mailer_name</strong>: Overrides the mailer name<br />
<strong>mime_version</strong>: Defaults to &#8220;1.0&#8243;, but may be explicitly given if needed<br />
<strong>part</strong>: Enables sending of multipart email messages by letting you define contenttype, template, and body variables.<br />
<strong>recipients</strong>: The address you are sending the message to. This requires a string, not the user object itself.<br />
<strong>sent_on</strong>: An explicit sent date<br />
<strong>subject</strong>: The subject for the message<br />
<strong>template</strong>: Specify the template name to use for the message.</p>
<p>To make HTML e-mail messages you must make sure your view template generates HTML and set the content type to html in your mailer method. Example:<br />
<code><br />
class MyMailer  "text/html", :body =&gt; render_message("signup_as_plain", :account =&gt; recipient)<br />
    part  "text_plain" do |p|<br />
      p.body = render_message("signup_as_plain", :account =&gt; recipient)<br />
      p.transfer_encoding = "base64"<br />
    end<br />
  end<br />
end</code></p>
<p><strong>Adding Attachments to an Email</strong><br />
<code>attachment :content_type =&gt; "image/jpeg", :body =&gt; File.read("example.jpg")</p>
<p>attachment "application/pdf" do |a|<br />
 a.body = generate_your_pdf_here()<br />
end</code></p>
<p><strong>Sending an E-mail</strong><br />
You should never try to call the instance of the methods directly. You need to call one of the two class methods generated for you based on the instance methods defined in your mailer class. Those two cass methods are deliver_ and create_. Let&#8217;s say you wrote a signup_notification instance method on a class named ApplicationMailer, you would want to use the code:<br />
<code><br />
ApplicationMailer.create_signup_notification("exampe@whatever.com")<br />
ApplicationMailer.deliver_signup_notification("example@whatever.com")<br />
</code><br />
ApplicationMailer.new.signup_notification(&#8220;example@whatever.com&#8221;) # WRONG!<br />
<strong><br />
Recieving E-mails</strong></p>
<p>To receive e-mails we will want to use the TMail::Mail class which is a ruby library for email processing. We will want to write a public method named &#8220;receive&#8221; on one of your application&#8217;s ActionMailer::Base subclass. It will make a TMail object instance as its single parameter. When there is an incoming e-mail to handle, you can call a class method named receive on your Mailer class. </p>
<p>Example:<code><br />
class MessageArchiver  email.subject, :body =&gt; email.body)<br />
   end<br />
end</code></p>
<p>The receiver class method can be the target for a Postfix recipe or any other mail handler process that can pipe the contents of the email to another process. The Rails runner script makes it easy to handle incoming mail:<br />
./script/runner &#8216;MessageArchiver,receive(STDIN.read)&#8217;<br />
This way, when the message is received, the receive class method would be fed the raw string content of the incoming email via STDIN.</p>
<p><strong>Configuration</strong><br />
Usually you will not have to configure anything special to get mail sending to work. Your production server will have sendmail installed and ActionMailer works fine with it. If you dont have sendmail installed you can try setting up rails to send email directly via SMTP. The ActionMailer::Base class has a hash named smtp_settings (server_settings prior to Rails 2.0) that holds configuration information for SMTP servers. You will want to make some changes, like the ones below, to your config/environment.rb fies:<code><br />
AcrionMailer::Base.smtp_settings = {<br />
:address =&gt; 'smtp.yourserver.com',<br />
:port =&gt; '25',<br />
:domain =&gt; 'yourserver.com',<br />
localhost.localdomain<br />
:user_name =&gt; 'user',<br />
:password =&gt; 'password',<br />
:authentification =&gt; :plain<br />
}</code></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/justinbgood.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/justinbgood.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justinbgood.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justinbgood.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justinbgood.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justinbgood.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justinbgood.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justinbgood.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justinbgood.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justinbgood.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justinbgood.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justinbgood.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justinbgood.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justinbgood.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justinbgood.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justinbgood.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=15&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justinbgood.wordpress.com/2008/07/24/actionmailer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43206c086c628fff708a5de2c0ba31b7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">justinbgood</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby Forms!</title>
		<link>http://justinbgood.wordpress.com/2008/07/24/ruby-forms/</link>
		<comments>http://justinbgood.wordpress.com/2008/07/24/ruby-forms/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 18:36:11 +0000</pubDate>
		<dc:creator>justinbgood</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[FormHelper]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[form_for]]></category>
		<category><![CDATA[form_remote_tag]]></category>
		<category><![CDATA[form_tag]]></category>
		<category><![CDATA[remote_form_form]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://justinbgood.wordpress.com/?p=10</guid>
		<description><![CDATA[Ruby on Rails has a very comprehensive method of dealing with site interactions and the changes they make to database models by using Forms. All FormHelpers will help make HTML forms and inputs for either an ActiveRecord base or for other explicit variables and instances. The four Rails methods used to create forms are: form_for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=10&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ruby on Rails has a very comprehensive method of dealing with site interactions and the changes they make to database models by using Forms. All FormHelpers will help make HTML forms and inputs for either an ActiveRecord base or for other explicit variables and instances. The four Rails methods used to create forms are:</p>
<p>form_for<br />
remote_form_for<br />
form_tag<br />
form_remote_tag</p>
<p>One of the most important aspects to know about forms are that the &#8220;form_for&#8221; and &#8220;remote_form_for&#8221; are both methods used when you want to present model based data. The &#8220;form_tag&#8221; and &#8220;form_remote_tag&#8221; are used for input tags based on explicit naming and values, unlike the form_for which requires an association to an ActiveRecord model.</p>
<p><strong>form_for:</strong></p>
<p>The &#8220;form_for&#8221; allows you to create forms based on models you&#8217;ve already created for your Rails application. For example, if you have a model named &#8216;User&#8217; tied to a database table, you will have many fields in that model you may want to create/change/delete. An example form_for is below:<br />
<code><br />
&lt;% form_for :user, :url =&gt; { :action =&gt; 'update', :id =&gt; @user.id } do |form| %&gt;<br />
User Name: &lt;%= form.text_field :user_name  &amp;&gt;<br />
&lt;%= submit_tag 'Update' %&gt;<br />
&lt;% end %&gt;<br />
</code><br />
The forms first first variable, :user, let&#8217;s the form know that we will be changing an object of User model. The :url hash tells us which method on the controller we will use when we submit the button . The :id parameter allows us to send an id to the controller which will be inside the params[:id] variable (inside the controller). You can send anything to the controller that is needed.</p>
<p>example <code>:url =&gt; { :action =&gt; 'new', :user_id =&gt; @user.id, :topic_id =&gt; @topic.id }</code>&#8230; we have just sent to the &#8216;new&#8217; function inside the user_controller a user_id ( called by params[:user_id] inside &#8216;new&#8217; ) and a topic_id ) called by params[:topic_id] )</p>
<p>The form_for&#8217;s content is contained inside of a block, which is seen in the &#8220;do |form|&#8221; Since we are in a block we can make calls against the &#8216;form&#8217; variable. This allows us to access to methods to change different sorts of form elements. As shown in the example above, we are putting our user_name column value into  text_field which maps into an input control.</p>
<p>Here is the example of our &#8220;update&#8221; method:<br />
<code><br />
def update<br />
user = User.find(params[:id])<br />
user.update_attributes(params[:user])<br />
redirect_to :action =&gt; 'form_form_test'<br />
end<br />
</code><br />
First we are finding the user from the database by grabbing the id from the :id parameter. Next we use Rails special update_attributes method to grab the users parameter representing the changes we made when we submitted our form and applies it to the model while saving the database.</p>
<p>Form helpers are designed to make working with models much easier compared to using just standard HTML elements by providing a set of methods for creating forms based on your models. This helper generates the HTML for forms, providing a method for each sort of input (e.g., text, password, select, and so on). When the form is submitted (i.e., when the user hits the submit button or form.submit is called via JavaScript), the form inputs will be bundled into the params object and passed back to the controller.</p>
<p><strong>remote_form_for</strong></p>
<p>The remote_form_for is based of the PrototypeHelper module. Prototype is a JavaScript library that provides DOM manipulation, Ajax functionality, other traditional JavaScript functions. PrototypeHelper creates helpers to make it more convenient to call functions from Prototype using Rails, including functionality to call remote Rails methods using Ajax. This allows you you to call actions in your controllers without reloading the page and still be able to update parts of it using injections into the DOM. To use the Prototype helpers you must include the Prototype JavaScript framework in your pages.</p>
<p>javascript_include_tag &#8216;prototype&#8217;</p>
<p>The remote_form_for reates a form that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement and a scope around a specific resource that is used as a base for questioning about values for the fields. One of the many defenitions of remote_form_for is</p>
<p>remote_form_for(record_or_name_or_array, *args, &amp;proc)</p>
<p><strong>form_tag</strong></p>
<p>form_tag starts a FORM , with its action attribute set to the URL passed at the url_for_options parameter. The :method option defaults to POST. Browsers can handle HTTP GET and POST but if you specify &#8220;put&#8221;, &#8220;delete&#8221; or any other HTTP verb,  a hidden input field will be inserted with the name _method and a  value corresponding to the method supplied. The :multipart option allows you to specify that you will be including file upload fields in the form submission and the server should be ready to handle those files accordingly. heres is the definition for a form_tag:</p>
<p>form_tag(url_for_options = {}, options = {}, #parameters_for_url, &amp;block)</p>
<p>example:<code><br />
form_tag('/posts')<br />
# =&gt; &lt;form action="/posts" method="post"&gt;</p>
<p>form_tag('/posts/1', :method =&gt; :put)<br />
# =&gt; &lt;form action="/posts/1" method="put"&gt;</p>
<p>form_tag('/upload', :multipart =&gt; true)<br />
# =&gt; &lt;form action="/upload" method="post" enctype="multipart/form-data"&gt;</p>
<p>&lt;% form_tag '/posts' do -%&gt;<br />
&lt;div&gt;&lt;%= submit_tag 'Save' %&gt;&lt;/div&gt;<br />
&lt;% end -%&gt;<br />
# =&gt; &lt;form action="/posts" method="post"&gt;&lt;div&gt;&lt;input type="submit" name="submit" value="Save" /&gt;&lt;/div&gt;&lt;/form&gt;</code><br />
<strong><br />
form_remote_tag</strong></p>
<p>the form_remote_tag returns a form tag that will submit using XMLHttpRequest in the background instead of the regular reloading POST. Even though it’s using JavaScript to serialize the form elements, the form submission will still have all the elements available in the params. The options for specifying the target with :url and defining callbacks is the same as link_to_remote. A &#8220;fall-through&#8221; target for browsers that don&#8217;t  execute JavaScript can be specified with the :action/:method options inside :html.</p>
<p>example: <code>form_remote_tag :html =&gt; { :action =&gt; url_for(:controller =&gt; "posts", :action =&gt; "edit") }</code></p>
<p>The Hash passed to the :html key is equivalent to the options 2nd argument in the FormTagHelper.form_tag method.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/justinbgood.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/justinbgood.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justinbgood.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justinbgood.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justinbgood.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justinbgood.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justinbgood.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justinbgood.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justinbgood.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justinbgood.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justinbgood.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justinbgood.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justinbgood.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justinbgood.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justinbgood.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justinbgood.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=10&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justinbgood.wordpress.com/2008/07/24/ruby-forms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43206c086c628fff708a5de2c0ba31b7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">justinbgood</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello Everyone!</title>
		<link>http://justinbgood.wordpress.com/2008/07/24/hello-everyone/</link>
		<comments>http://justinbgood.wordpress.com/2008/07/24/hello-everyone/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 17:58:21 +0000</pubDate>
		<dc:creator>justinbgood</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[bryant]]></category>
		<category><![CDATA[justin]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://justinbgood.wordpress.com/?p=3</guid>
		<description><![CDATA[Hello everyone. My name is Justin. I am a computer scientist at UCSB and a professional longboarder for comet skateboards. I am hoping that I will be able to write some blogs that will be able to shed some light on concepts and questions directed towards newer users to Ruby on Rails. As I am [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=3&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hello everyone. My name is Justin. I am a computer scientist at UCSB and a professional longboarder for comet skateboards. I am hoping that I will be able to write some blogs that will be able to shed some light on concepts and questions directed towards newer users to Ruby on Rails. As I am still quite new to Ruby on Rails, this will be a learning process for myself as well. If you would like to learn more about me and my skating, please feel free to check me out at www.cometskateboards.com. We have many cool videos and bogs posted. Also if you are looking for some harder, more in depth Ruby on Rails information, check out the company I work for at www.inigral.com. Thanks for stopping by and enjoy!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/justinbgood.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/justinbgood.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justinbgood.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justinbgood.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justinbgood.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justinbgood.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justinbgood.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justinbgood.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justinbgood.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justinbgood.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justinbgood.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justinbgood.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justinbgood.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justinbgood.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justinbgood.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justinbgood.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justinbgood.wordpress.com&amp;blog=4316650&amp;post=3&amp;subd=justinbgood&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justinbgood.wordpress.com/2008/07/24/hello-everyone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43206c086c628fff708a5de2c0ba31b7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">justinbgood</media:title>
		</media:content>
	</item>
	</channel>
</rss>
