<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.6" -->
<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/"
	>

<channel>
	<title>Technology Voodoo</title>
	<link>http://technologyvoodoo.com</link>
	<description>computer black magic</description>
	<pubDate>Thu, 25 Jan 2007 16:58:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.6</generator>
	<language>en</language>
			<item>
		<title>Hacking GeSHi to Fix the Height of Code Blocks</title>
		<link>http://technologyvoodoo.com/articles/hacking-geshi-to-fix-the-height-of-code-blocks</link>
		<comments>http://technologyvoodoo.com/articles/hacking-geshi-to-fix-the-height-of-code-blocks#comments</comments>
		<pubDate>Wed, 17 Jan 2007 15:52:51 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
	<dc:subject>Uncategorized</dc:subject><dc:subject>code</dc:subject><dc:subject>hack</dc:subject><dc:subject>php</dc:subject><dc:subject>wordpress</dc:subject>
		<guid isPermaLink="false">http://technologyvoodoo.com/articles/hacking-geshi-to-fix-the-height-of-code-blocks</guid>
		<description><![CDATA[When setting up this blog, which is powered by WordPress, I knew that there were going to be a lot of posts about code. In order to make the code readable, I wanted to implement some sort of syntax highlighting. I eventually came across a great plugin called Simple Syntax, which is powered by the [...]]]></description>
			<content:encoded><![CDATA[<p>When setting up this blog, which is powered by <a href="http://wordpress.org/">WordPress</a>, I knew that there were going to be a lot of posts about code. In order to make the code readable, I wanted to implement some sort of syntax highlighting. I eventually came across a great plugin called <a href="http://www.devel.narrabilis.com/posts/5">Simple Syntax</a>, which is powered by the <a href="http://qbnz.com/highlighter/">GeSHi</a> highlighter engine. However, it needed a few modifications before it did exactly what I wanted it to do.</p>
<p><a id="more-11"></a><!--adsense--></p>
<p>The first thing I wanted to do was enclose my code in blocks that could scroll left and right, up and down, so that long blocks of code didn&#8217;t take up a huge amount of space, and so that wide blocks of code didn&#8217;t wrap or reach outside of the content area.</p>
<p>After putting my waders on and going through some of the code, I found the function that generates the headers for my code blocks. With Simply Syntax, the file is &#8220;SimpleSyntax/geshi/geshi.php&#8221;. Do a text search for &#8220;function header&#8221; and you&#8217;ll get the code block we&#8217;re going to hack:</p>
<pre style="height: 300px;" class="code"><span style="color: #000000; font-weight: bold;">function</span> <a href="http://www.php.net/header"><span style="color: #000066;">header</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// Get attributes needed</span>
  <span style="color: #0000ff;">$attributes</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">get_attributes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #0000ff;">$ol_attributes</span> = <span style="color: #ff0000;">''</span>;
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers_start</span> != <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$ol_attributes</span> .= <span style="color: #ff0000;">' start=&quot;'</span> . <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers_start</span> . <span style="color: #ff0000;">'&quot;'</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">// Get the header HTML</span>
  <span style="color: #0000ff;">$header</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">format_header_content</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>GESHI_HEADER_NONE == <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers</span> != GESHI_NO_LINE_NUMBERS<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;$header&lt;ol$ol_attributes&gt;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$header</span>;
  <span style="color: #66cc66;">&#125;</span>

  <span style="color: #808080; font-style: italic;">// Work out what to return and do it</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers</span> != GESHI_NO_LINE_NUMBERS<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_PRE<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;pre$attributes&gt;$header&lt;ol$ol_attributes&gt;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_DIV<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;div$attributes&gt;$header&lt;ol$ol_attributes&gt;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_PRE<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;pre$attributes&gt;$header&quot;</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_DIV<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;div$attributes&gt;$header&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>The first thing to do is make our code blocks part of a certain class, so that we can style them with CSS. Just add &#8220;class=\&#8221;code\&#8221;" to the following parts of the header function (we also have to remove the $attributes variable from the header. If not, it&#8217;s going to add its own class tags, which we don&#8217;t want):</p>
<pre class="code">  <span style="color: #808080; font-style: italic;">// Work out what to return and do it</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers</span> != GESHI_NO_LINE_NUMBERS<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_PRE<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;pre class=<span style="color: #000099; font-weight: bold;">\&quot;</span>code<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;$header&lt;ol$ol_attributes&gt;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_DIV<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;div$attributes&gt;$header&lt;ol$ol_attributes&gt;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_PRE<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;pre class=<span style="color: #000099; font-weight: bold;">\&quot;</span>code<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;$header&quot;</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_DIV<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;div$attributes&gt;$header&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span></pre>
<p>Alright, now that our code blocks have their own class, let&#8217;s add the CSS to put them into blocks:</p>
<pre class="code"><span style="color: #6666ff;">.code </span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span>: #f0f0f0;
	<span style="color: #000000; font-weight: bold;">border</span>: 1px <span style="color: #993333;">solid</span> #C3CED9;
	<span style="color: #000000; font-weight: bold;">padding</span>: 0px 0px <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span>;
	<span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #cc66cc;">100</span>%;
	<span style="color: #000000; font-weight: bold;">color</span>: #<span style="color: #cc66cc;">000</span>;
	<span style="color: #000000; font-weight: bold;">text-align</span>: <span style="color: #000000; font-weight: bold;">left</span>;
	<span style="color: #000000; font-weight: bold;">font-size</span>:<span style="color: #cc66cc;">1</span>.00em;
	<span style="color: #000000; font-weight: bold;">overflow</span>:<span style="color: #993333;">auto</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>So now our code is put into nice blocks that scroll horizontally (if you want, you can change the width to a fixed pixel amount. 100% worked best for this layout). But, if you post a block of code with many lines, it&#8217;s going to make a very tall code block. What we want to do is determine the number of lines in a block of code, and, if it&#8217;s above a certain number, fix the height of the code block so that it scrolls vertically.</p>
<p>Back to our &#8220;header&#8221; function, here&#8217;s the code that will count the number of lines in a code block:</p>
<pre class="code"><a href="http://www.php.net/substr_count"><span style="color: #000066;">substr_count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">source</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span></pre>
<p>So all we have to do is check to see if this code returns a number above a certain value and, if it does, set a variable that will contain our height-fixing code.</p>
<pre class="code"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/substr_count"><span style="color: #000066;">substr_count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">source</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$maxsize</span> = <span style="color: #ff0000;">&quot; style=<span style="color: #000099; font-weight: bold;">\&quot;</span>height: 300px;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>Kid stuff. Now we just add our $maxsize variable to the headers, and we&#8217;re done. Here&#8217;s the final code:</p>
<p><!--adsense--></p>
<pre style="height: 300px;" class="code"><span style="color: #000000; font-weight: bold;">function</span> <a href="http://www.php.net/header"><span style="color: #000066;">header</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// hack to fix height if certain number of lines</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/substr_count"><span style="color: #000066;">substr_count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">source</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$maxsize</span> = <span style="color: #ff0000;">&quot; style=<span style="color: #000099; font-weight: bold;">\&quot;</span>height: 300px;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">$ol_attributes</span> = <span style="color: #ff0000;">''</span>;
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers_start</span> != <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$ol_attributes</span> .= <span style="color: #ff0000;">' start=&quot;'</span> . <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers_start</span> . <span style="color: #ff0000;">'&quot;'</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">// Get the header HTML</span>
  <span style="color: #0000ff;">$header</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">format_header_content</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>GESHI_HEADER_NONE == <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers</span> != GESHI_NO_LINE_NUMBERS<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;$header&lt;ol$ol_attributes&gt;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$header</span>;
  <span style="color: #66cc66;">&#125;</span>

  <span style="color: #808080; font-style: italic;">// Work out what to return and do it</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">line_numbers</span> != GESHI_NO_LINE_NUMBERS<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_PRE<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;pre$maxsize class=<span style="color: #000099; font-weight: bold;">\&quot;</span>code<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;$header&lt;ol$ol_attributes&gt;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_DIV<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;div$attributes&gt;$header&lt;ol$ol_attributes&gt;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_PRE<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;pre$maxsize class=<span style="color: #000099; font-weight: bold;">\&quot;</span>code<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;$header&quot;</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">header_type</span> == GESHI_HEADER_DIV<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;div$attributes&gt;$header&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://technologyvoodoo.com/articles/hacking-geshi-to-fix-the-height-of-code-blocks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>User Registration, with Email Confirmation, in Ruby on Rails</title>
		<link>http://technologyvoodoo.com/articles/user-registration-with-email-confirmation-in-ruby-on-rails</link>
		<comments>http://technologyvoodoo.com/articles/user-registration-with-email-confirmation-in-ruby-on-rails#comments</comments>
		<pubDate>Tue, 16 Jan 2007 23:08:47 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
	<dc:subject>Uncategorized</dc:subject><dc:subject>code</dc:subject><dc:subject>rubyonrails</dc:subject>
		<guid isPermaLink="false">http://technologyvoodoo.com/articles/user-registration-with-email-confirmation-in-ruby-on-rails</guid>
		<description><![CDATA[User registration is function that is more or less common to all web applications. If your app has users, there has to be a way for them to register, right? Sometimes you want to add the extra step of confirming the user&#8217;s email address, to ensure that they have entered a valid address which they [...]]]></description>
			<content:encoded><![CDATA[<p>User registration is function that is more or less common to all web applications. If your app has users, there has to be a way for them to register, right? Sometimes you want to add the extra step of confirming the user&#8217;s email address, to ensure that they have entered a valid address which they have access to. Here&#8217;s a way to handle user registration in <a href="http://www.rubyonrails.org">Ruby on Rails</a> that&#8217;s secure, robust, and handles email validation.</p>
<p><a id="more-10"></a> <!--adsense--></p>
<p>We&#8217;re going to start from scratch, so the first thing to do is create our application. Navigate to the directory where you want your app to live, and type</p>
<p class="plaintext">rails appname</p>
<p>where &#8220;appname&#8221; is the name you want to give your application.</p>
<p>Ok, now that we have our application set up, let&#8217;s create the models and controllers that we know we&#8217;ll be using. The only model we need for this example is a User model, and we&#8217;ll keep things simple and use a single controller called &#8220;Main.&#8221;</p>
<p class="plaintext">script/generate model User<br />
script/generate controller Main</p>
<p>Now we&#8217;ve got our model and controller set up. We&#8217;re going to use <a href="http://www.rubyonrails.org/api/classes/ActiveRecord/Migration.html">database migrations</a> for this tutorial; if you&#8217;re not used to using them, don&#8217;t worry, we&#8217;ll outline each step that you need to follow. To get started, open the file &#8220;/db/migrate/001_create_users.rb&#8221;. This file was created for us when we ran &#8220;script/generate model User&#8221;, and, for our purposes, it&#8217;s going to contain the database information about our User model.</p>
<p>What kind of database fields will we need for our User model? Obviously, we&#8217;ll need a username and password field, as well as an email address. Let&#8217;s also add a boolen &#8220;email_confirmed,&#8221; so that we can keep track of which users have confirmed their email address, and which haven&#8217;t. Also, instead of a &#8220;password&#8221; field, let&#8217;s use &#8220;hashed_password&#8221;, since we&#8217;re going to <a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function">hash</a> the password before it gets stored in the database (we&#8217;ll also put a limit of 40 on the field because that will be the size of our hash). Edit your 001_create_users.rb so that it looks like this:</p>
<pre class="code"><span style="color:#9966CC; font-weight:bold;">class</span> CreateUsers &lt; ActiveRecord::Migration
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table :users <span style="color:#9966CC; font-weight:bold;">do</span> |t|
      t.<span style="color:#9900CC;">column</span> <span style="color:#996600;">&quot;username&quot;</span>, :<span style="color:#CC0066; font-weight:bold;">string</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#996600;">&quot;hashed_password&quot;</span>, :<span style="color:#CC0066; font-weight:bold;">string</span>, :limit =&gt; <span style="color:#006666;">40</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#996600;">&quot;email&quot;</span>, :<span style="color:#CC0066; font-weight:bold;">string</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#996600;">&quot;email_confirmed&quot;</span>, :boolean, :default =&gt; <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table :users
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>Now that our database migration is set up, we can go ahead and use it to generate our database schema. First, edit &#8220;/config/database.yml&#8221; so that it points to the database you&#8217;re using. Once you have the proper values entered there, go to the root of your app and type</p>
<p class="plaintext">rake db:migrate</p>
<p>Once the rake finishes, open up your database, and note that our &#8220;users&#8221; table was created, along with all the fields that we wanted. Notice also that the rake task created an &#8220;id&#8221; field, even though we didn&#8217;t define it ourselves in the migration file.</p>
<p>Now that our database is set up, let&#8217;s create our actions. We&#8217;ll need three actions, an &#8220;index&#8221; action that users will see when they go to the root section of our site, a register action, and an action that confirms the email address. Open up &#8220;/app/controllers/main_controller.rb&#8221; and add the following definitions</p>
<pre class="code"><span style="color:#9966CC; font-weight:bold;">class</span> MainController &lt; ApplicationController
&nbsp;
  <span style="color:#008000; font-style:italic;"># the root index action for our application</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> index
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># allows guests to create a new User</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> register
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># confirm an email address</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> confirm_email
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>Since we have actions now, let&#8217;s set up our routes and our views. To configure the routes, open up &#8220;/app/config/routes.rb&#8221; and add the following routes:</p>
<pre class="code">  map.<span style="color:#9900CC;">connect</span> '', :controller =&gt; <span style="color:#996600;">&quot;main&quot;</span>, :action =&gt; <span style="color:#996600;">&quot;index&quot;</span>
  map.<span style="color:#9900CC;">connect</span> 'register', :controller =&gt; <span style="color:#996600;">&quot;main&quot;</span>, :action =&gt; <span style="color:#996600;">&quot;register&quot;</span>
  map.<span style="color:#9900CC;">connect</span> 'confirm_email/:hash', :controller =&gt; <span style="color:#996600;">&quot;main&quot;</span>, :action =&gt; <span style="color:#996600;">&quot;confirm_email&quot;</span></pre>
<p>Now that our routes are hooked up, let&#8217;s create our views. Create the file &#8220;/app/views/main/index.rhtml&#8221;</p>
<pre class="code">&lt;h1&gt;Welcome!&lt;/h1&gt;
&lt;p&gt;
Click &lt;%= link_to <span style="color:#996600;">&quot;here&quot;</span>, :controller =&gt; <span style="color:#996600;">&quot;main&quot;</span>, :action =&gt; <span style="color:#996600;">&quot;register&quot;</span> %&gt; to register.
&lt;/p&gt;</pre>
<p>and &#8220;/app/views/main/register.rhtml&#8221;</p>
<pre class="code">&lt;%= error_messages_for 'user' %&gt;
&lt;h1&gt;Register&lt;/h1&gt;
&lt;%= form_tag %&gt;
&lt;p&gt;username&lt;br/&gt;
&lt;%= text_field <span style="color:#996600;">&quot;user&quot;</span>, <span style="color:#996600;">&quot;username&quot;</span>, <span style="color:#996600;">&quot;maxlength&quot;</span> =&gt; <span style="color:#006666;">20</span>, <span style="color:#996600;">&quot;size&quot;</span> =&gt; <span style="color:#006666;">20</span> %&gt;&lt;/p&gt;
&lt;p&gt;email address&lt;br/&gt;
&lt;%= text_field <span style="color:#996600;">&quot;user&quot;</span>, <span style="color:#996600;">&quot;email&quot;</span>, <span style="color:#996600;">&quot;maxlength&quot;</span> =&gt; <span style="color:#006666;">50</span>, <span style="color:#996600;">&quot;size&quot;</span> =&gt; <span style="color:#006666;">20</span> %&gt;&lt;/p&gt;
&lt;p&gt;password&lt;br/&gt;
&lt;%= password_field <span style="color:#996600;">&quot;user&quot;</span>, <span style="color:#996600;">&quot;password&quot;</span>, <span style="color:#996600;">&quot;maxlength&quot;</span> =&gt; <span style="color:#006666;">20</span>, <span style="color:#996600;">&quot;size&quot;</span> =&gt; <span style="color:#006666;">20</span> %&gt;&lt;/p&gt;
&lt;p&gt;confirm password&lt;br/&gt;
&lt;%= password_field <span style="color:#996600;">&quot;user&quot;</span>, <span style="color:#996600;">&quot;confirm_password&quot;</span>, <span style="color:#996600;">&quot;maxlength&quot;</span> =&gt; <span style="color:#006666;">20</span>, <span style="color:#996600;">&quot;size&quot;</span> =&gt; <span style="color:#006666;">20</span> %&gt;&lt;/p&gt;
&lt;input type=<span style="color:#996600;">&quot;submit&quot;</span> value=<span style="color:#996600;">&quot;sign up&quot;</span> /&gt;&lt;/p&gt;
&lt;%= end_form_tag %&gt;
&nbsp;</pre>
<p>We&#8217;re going to try to get away with not creating a view for our &#8220;confirm_email&#8221; action, so let&#8217;s leave alone that for now.</p>
<p>Alright, we&#8217;ve got our views set up, so let&#8217;s start writing some of the application code. Let&#8217;s start with the &#8220;register&#8221; action of our &#8220;main&#8221; controller. Basically, our registration function needs to grab the user parameters from the form in our &#8220;register.rhtml&#8221; view, create the user, and redirect to the login form. Simple enough. Here&#8217;s the code</p>
<pre class="code">  <span style="color:#008000; font-style:italic;"># allows guests to create a new User</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> register
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> request.<span style="color:#9900CC;">get</span>?
      @user = User.<span style="color:#9900CC;">new</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      @user = User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span>:user<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>

      <span style="color:#9966CC; font-weight:bold;">if</span> @user.<span style="color:#9900CC;">save</span>
        flash<span style="color:#006600; font-weight:bold;">&#91;</span>:notice<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Thank you for registering! We have sent a confirmation email to #{@user.email} with instructions on how to validate your account.&quot;</span>
        redirect_to<span style="color:#006600; font-weight:bold;">&#40;</span>:action =&gt; <span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span></pre>
<p>Now, you&#8217;ll notice two things that are very wrong. First of all, we&#8217;re telling the user that we sent him a confirmation email, but clearly we did no such thing. Also, we have no validation in our user model! Nothing is checking to make sure that the email address entered is properly formatted, that the username isn&#8217;t taken, that the password and confirm_password fields match, etc. Let&#8217;s open up our user model and add this validation now.</p>
<pre class="code"><span style="color:#9966CC; font-weight:bold;">class</span> User &lt; ActiveRecord::Base
  validates_presence_of :username, :email
  validates_uniqueness_of :username
  validates_length_of :username, :minimum =&gt; <span style="color:#006666;">4</span>
&nbsp;
  attr_accessor :password, :confirm_password
&nbsp;
  <span style="color:#008000; font-style:italic;"># callback hooks</span>

    <span style="color:#008000; font-style:italic;"># validate that password and confirm_password match, and that email is proper format</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> validate_on_create
      @email_format = Regexp.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>/^<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>^@\s<span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006600; font-weight:bold;">&#41;</span>@<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>?:<span style="color:#006600; font-weight:bold;">&#91;</span>-a-z0<span style="color:#006666;">-9</span><span style="color:#006600; font-weight:bold;">&#93;</span>+\.<span style="color:#006600; font-weight:bold;">&#41;</span>+<span style="color:#006600; font-weight:bold;">&#91;</span>a-z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span>,<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>$/<span style="color:#006600; font-weight:bold;">&#41;</span>
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:email, <span style="color:#996600;">&quot;must be a valid format&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> @email_format.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>email<span style="color:#006600; font-weight:bold;">&#41;</span>
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:confirm_password, <span style="color:#996600;">&quot;does not match&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> password == confirm_password
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:password, <span style="color:#996600;">&quot;cannot be blank&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> !password <span style="color:#9966CC; font-weight:bold;">or</span> password.<span style="color:#9900CC;">length</span> &gt; <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>You&#8217;ll notice that we also added the line &#8220;attr_accessor :password, :confirm_password&#8221;. This is because we did not create a password or confirm_password field in our database, but we still want to access these properties on our user object. For this, we use the ruby method <a href="http://www.ruby-doc.org/core/classes/Module.html#M001734">attr_accessor</a>, which in this case essentially creates model properties for us which we can use like any other, but which are not stored in the database anywhere.</p>
<p>Now, we still need to figure out how to hash the password before it gets stored in the database. We could put this in our controller, but it really belongs in our user model. Rails provides us with lots of handy callback hooks, so let&#8217;s use one. We want to hash the password before the user is created, so we&#8217;ll use the hook &#8220;before_create&#8221;. We&#8217;ll also want to create a private function that does the actual hashing for us, and we&#8217;ll have to add a line at the top of our class to include <a href="http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ext/digest/sha1/?pathrev=3897">the ruby class that does the hashing</a>.</p>
<p><!--adsense-->
<pre style="height: 300px;" class="code"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest/sha1&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> User &lt; ActiveRecord::Base
  validates_presence_of :username, :email
  validates_uniqueness_of :username
  validates_length_of :username, :minimum =&gt; <span style="color:#006666;">4</span>

  attr_accessor :password, :confirm_password
&nbsp;
  <span style="color:#008000; font-style:italic;"># callback hooks</span>

    <span style="color:#008000; font-style:italic;"># validate that password and confirm_password match, and that email is proper format</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> validate_on_create
      @email_format = Regexp.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>/^<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>^@\s<span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006600; font-weight:bold;">&#41;</span>@<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>?:<span style="color:#006600; font-weight:bold;">&#91;</span>-a-z0<span style="color:#006666;">-9</span><span style="color:#006600; font-weight:bold;">&#93;</span>+\.<span style="color:#006600; font-weight:bold;">&#41;</span>+<span style="color:#006600; font-weight:bold;">&#91;</span>a-z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span>,<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>$/<span style="color:#006600; font-weight:bold;">&#41;</span>
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:email, <span style="color:#996600;">&quot;must be a valid format&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> @email_format.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>email<span style="color:#006600; font-weight:bold;">&#41;</span>
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:confirm_password, <span style="color:#996600;">&quot;does not match&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> password == confirm_password
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:password, <span style="color:#996600;">&quot;cannot be blank&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> !password <span style="color:#9966CC; font-weight:bold;">or</span> password.<span style="color:#9900CC;">length</span> &gt; <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># hash password before create</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> before_create
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">hashed_password</span> = User.<span style="color:#9900CC;">hash_password</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>

&nbsp;
  private
&nbsp;
  <span style="color:#008000; font-style:italic;"># hash password for storage in database</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">hash_password</span><span style="color:#006600; font-weight:bold;">&#40;</span>password<span style="color:#006600; font-weight:bold;">&#41;</span>
    Digest::SHA1.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span>password<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>

<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>Now, we could stop here, but let&#8217;s make our user model a little more robust and secure. Let&#8217;s remove cleartext passwords from memory after the user is created, and let&#8217;s scrub email addresses and usernames to remove spaces and convert to lowercase. To do these two things, we&#8217;ll add two more callback hooks and another private function.</p>
<pre style="height: 300px;" class="code"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest/sha1&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> User &lt; ActiveRecord::Base
  validates_presence_of :username, :email
  validates_uniqueness_of :username
  validates_length_of :username, :minimum =&gt; <span style="color:#006666;">4</span>

  attr_accessor :password, :confirm_password
&nbsp;
  <span style="color:#008000; font-style:italic;"># callback hooks</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># clean up email and username before validation</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> before_validation
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">email</span> = User.<span style="color:#9900CC;">clean_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">email</span> || <span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">username</span> = User.<span style="color:#9900CC;">clean_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">username</span> || <span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>

    <span style="color:#008000; font-style:italic;"># validate that password and confirm_password match, and that email is proper format</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> validate_on_create
      @email_format = Regexp.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>/^<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>^@\s<span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006600; font-weight:bold;">&#41;</span>@<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>?:<span style="color:#006600; font-weight:bold;">&#91;</span>-a-z0<span style="color:#006666;">-9</span><span style="color:#006600; font-weight:bold;">&#93;</span>+\.<span style="color:#006600; font-weight:bold;">&#41;</span>+<span style="color:#006600; font-weight:bold;">&#91;</span>a-z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span>,<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>$/<span style="color:#006600; font-weight:bold;">&#41;</span>
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:email, <span style="color:#996600;">&quot;must be a valid format&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> @email_format.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>email<span style="color:#006600; font-weight:bold;">&#41;</span>
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:confirm_password, <span style="color:#996600;">&quot;does not match&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> password == confirm_password
      errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>:password, <span style="color:#996600;">&quot;cannot be blank&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> !password <span style="color:#9966CC; font-weight:bold;">or</span> password.<span style="color:#9900CC;">length</span> &gt; <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># hash password before create</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> before_create
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">hashed_password</span> = User.<span style="color:#9900CC;">hash_password</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>

    <span style="color:#008000; font-style:italic;"># after creation, clear password from memory</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> after_create
      @password = <span style="color:#0000FF; font-weight:bold;">nil</span>
      @confirm_password = <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>

&nbsp;
  private
&nbsp;
  <span style="color:#008000; font-style:italic;"># hash password for storage in database</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">hash_password</span><span style="color:#006600; font-weight:bold;">&#40;</span>password<span style="color:#006600; font-weight:bold;">&#41;</span>
    Digest::SHA1.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span>password<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># clean string to remove spaces and force lowercase</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">clean_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">string</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#9900CC;">downcase</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot; &quot;</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>

<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>Alright, our user model is all done. It takes care of validation, hashes the password, cleans up email addresses and usernames, and minimizes the exposure of cleartext passwords. We&#8217;ve done a lot of work, so stand up, stretch, grab a beer, and come back.</p>
<p>Now, it&#8217;s time to add the mailer object that will send out the confirmation email. Before we do that, however, let&#8217;s take a moment to step back and think about how we&#8217;re going to handle email confirmation from an abstract level.</p>
<p>For email registration, we need to make sure that whatever method we use, the user cannot cheat and manually enter the URL to validate his email (no typing http://website.com/confirm_email/username). To prevent this, we need to use some kind of randomly-generated string. Now, we could just randomly generate a string and stick it in the database in our &#8220;users&#8221; table, but that&#8217;s sort of inelegant. Instead, it would be best if we could create some kind of seemingly random string, which the user cannot reverse engineer, but which we can recreate given known values. This is exactly what a hash is meant to be used for!</p>
<p>Let&#8217;s say, for example, that a user registers with the username &#8220;zaphod&#8221;. Converted to a hash using our algorithm, &#8220;zaphod&#8221; becomes &#8220;79118ddf1f07f13c3ddaf659f835f1ce4e1161f3&#8243;. We could then use that hash as a confirmation code, generating a confirmation url of &#8220;http://website.com/confirm_email/79118ddf1f07f13c3ddaf659f835f1ce4e1161f3&#8243;. When our app handles this URL, it just has to go through our list of users, look for one whose username, put through the hash function, matches &#8220;79118ddf1f07f13c3ddaf659f835f1ce4e1161f3&#8243;, and it will know which user is attempting to confirm! Note that the hash function is one-way, so we cannot just &#8220;unhash&#8221; the hash and match it up to a username.</p>
<p>Since we want to be secure, let&#8217;s go a step further. Instead of just hashing the username, let&#8217;s hash their username plus a secret word which we define. This prevents users from figuring out how we generate our hash (since they don&#8217;t know the inputs), and generating the hash themselves.</p>
<p>In our &#8220;main&#8221; controller, add the line to include the Ruby hash class, and add our private function for generating email confirmation hashes:</p>
<pre style="height: 300px;" class="code"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest/sha1&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MainController &lt; ApplicationController
&nbsp;
  <span style="color:#008000; font-style:italic;"># the root index action for our application</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> index
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># allows guests to create a new User</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> register

    <span style="color:#9966CC; font-weight:bold;">if</span> request.<span style="color:#9900CC;">get</span>?
      @user = User.<span style="color:#9900CC;">new</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      @user = User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span>:user<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>

      <span style="color:#9966CC; font-weight:bold;">if</span> @user.<span style="color:#9900CC;">save</span>
        flash<span style="color:#006600; font-weight:bold;">&#91;</span>:notice<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Thank you for registering! We have sent a confirmation email to #{@user.email} with instructions on how to validate your account.&quot;</span>
        redirect_to<span style="color:#006600; font-weight:bold;">&#40;</span>:action =&gt; <span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># confirm an email address</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> confirm_email
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
  private

  <span style="color:#008000; font-style:italic;"># create a hash to use when confirming User email addresses</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> confirmation_hash<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">string</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    Digest::SHA1.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">string</span> + <span style="color:#996600;">&quot;secret word&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>Great, so now we know how we&#8217;re going to handle email confirmation, and we&#8217;ve got our function all set up to generate our hashes. Let&#8217;s create our mail robot that&#8217;s going to send out our emails. In the console, type</p>
<p class="plaintext">script/generate mailer MailRobot</p>
<p>This generates a model called MailRobot, which is going to handle all of our email stuff. The way <a href="http://rubyonrails.com/rails/classes/ActionMailer/Base.html">ActionMailer</a> works is that, for each email we want our app to be able to send, we need to create an action within our mailer model and a view to go along with it. Open up &#8220;/app/models/mail_robot.rb&#8221; and let&#8217;s add that action now.</p>
<pre class="code"><span style="color:#9966CC; font-weight:bold;">class</span> MailRobot &lt; ActionMailer::Base
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> confirmation_email<span style="color:#006600; font-weight:bold;">&#40;</span>user,hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># email header info MUST be added here</span>
    @recipients = user.<span style="color:#9900CC;">email</span>
    @from = <span style="color:#996600;">&quot;fromaddress@website-url-here.com&quot;</span>
    @subject = <span style="color:#996600;">&quot;Confirm email address&quot;</span>

    <span style="color:#008000; font-style:italic;"># email body substitutions go here</span>
    @body<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;username&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = user.<span style="color:#9900CC;">username</span>
    @body<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;hash&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = hash
  <span style="color:#9966CC; font-weight:bold;">end</span>

<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>Pretty simple. We&#8217;re just passing our user object and our confirmation hash to the action, setting up the &#8220;to,&#8221; &#8220;from,&#8221; and &#8220;subject&#8221; of the email, and defining some variables that we&#8217;re going to use in our view. Let&#8217;s create that view now; we&#8217;ll need to create &#8220;/app/views/mail_robot/confirmation_email.rhtml&#8221;</p>
<pre class="code">&lt;%= @username %&gt;,
&nbsp;
Thank you <span style="color:#9966CC; font-weight:bold;">for</span> registering with our website. <span style="color:#9900CC;">To</span> confirm your email address, please visit the following address:
&nbsp;
http://www.<span style="color:#9900CC;">website</span>-url-here.<span style="color:#9900CC;">com</span>/confirm_email/&lt;%= @hash %&gt;
&nbsp;
- Friendly Mail Robot
&nbsp;</pre>
<p>One last thing we need to set up to get our mailer working. We need to edit our &#8220;/app/config/environment.rb&#8221; and add our mail settings to the end of the file. If you&#8217;re using DreamHost, your settings will look like this:</p>
<pre class="code">ActionMailer::Base.<span style="color:#9900CC;">delivery_method</span> = :sendmail
ActionMailer::Base.<span style="color:#9900CC;">server_settings</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
  :address  =&gt; <span style="color:#996600;">&quot;mail.website-url-here.com&quot;</span>,
  :port  =&gt; <span style="color:#006666;">25</span>,
  :domain  =&gt; <span style="color:#996600;">&quot;website-url-here.com&quot;</span>,
  :user_name  =&gt; <span style="color:#996600;">&quot;mXXXXXXX&quot;</span>,
  :password  =&gt; <span style="color:#996600;">&quot;password&quot;</span>,
  :authentication  =&gt; :login
    <span style="color:#006600; font-weight:bold;">&#125;</span>
ActionMailer::Base.<span style="color:#9900CC;">perform_deliveries</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
ActionMailer::Base.<span style="color:#9900CC;">raise_delivery_errors</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
ActionMailer::Base.<span style="color:#9900CC;">default_charset</span> = <span style="color:#996600;">&quot;utf-8&quot;</span>
&nbsp;</pre>
<p>If you&#8217;re not using DreamHost, see <a href="http://wiki.rubyonrails.org/rails/pages/HowToSendEmailsWithActionMailer">this tutorial</a> for help on getting these settings right.</p>
<p>Ok! Our mailer is all set up, so we can now go back to our &#8220;register&#8221; action in our &#8220;main&#8221; controller and add the code that will actually send out an email to our new user.</p>
<pre class="code">  <span style="color:#008000; font-style:italic;"># allows guests to create a new User</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> register
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> request.<span style="color:#9900CC;">get</span>?
      @user = User.<span style="color:#9900CC;">new</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      @user = User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span>:user<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>

      <span style="color:#9966CC; font-weight:bold;">if</span> @user.<span style="color:#9900CC;">save</span>
        MailRobot::deliver_confirmation_email<span style="color:#006600; font-weight:bold;">&#40;</span>@user, confirmation_hash<span style="color:#006600; font-weight:bold;">&#40;</span>@user.<span style="color:#9900CC;">username</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>

        flash<span style="color:#006600; font-weight:bold;">&#91;</span>:notice<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Thank you for registering! We have sent a confirmation email to #{@user.email} with instructions on how to validate your account.&quot;</span>
        redirect_to<span style="color:#006600; font-weight:bold;">&#40;</span>:action =&gt; <span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span></pre>
<p>Now when a new user registers, they will get a confirmation email containing the confirmation hash we generated, formed as a url to our &#8220;confirm_email&#8221; action within our main controller. All that&#8217;s left to do is write that action.</p>
<p>What do we need this action to do? Basically, it just needs to go through all the users in our database, and for each user, generate a confirmation hash using that user&#8217;s username. Then, it needs to compare that hash to the hash that it&#8217;s receiving. If it finds a match, it should mark that user&#8217;s email address as confirmed, log that user in, and redirect to our index. Here&#8217;s the code:</p>
<pre class="code">  <span style="color:#008000; font-style:italic;"># confirm an email address</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> confirm_email
&nbsp;
    @users = User.<span style="color:#9900CC;">find</span> :all

    <span style="color:#9966CC; font-weight:bold;">for</span> user <span style="color:#9966CC; font-weight:bold;">in</span> @users
      <span style="color:#008000; font-style:italic;"># if the passed hash matches up with a User, confirm him, log him in, set proper flash[:notice], and stop looking</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> confirmation_hash<span style="color:#006600; font-weight:bold;">&#40;</span>user.<span style="color:#9900CC;">username</span><span style="color:#006600; font-weight:bold;">&#41;</span> == params<span style="color:#006600; font-weight:bold;">&#91;</span>:hash<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">and</span> !user.<span style="color:#9900CC;">email_confirmed</span>
        user.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span>:email_confirmed =&gt; <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        session<span style="color:#006600; font-weight:bold;">&#91;</span>:user_id<span style="color:#006600; font-weight:bold;">&#93;</span> = user.<span style="color:#9900CC;">id</span>
        flash<span style="color:#006600; font-weight:bold;">&#91;</span>:notice<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Thank you for validating your email.&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">break</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    redirect_to<span style="color:#006600; font-weight:bold;">&#40;</span>:action =&gt; <span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span></pre>
<p>That&#8217;s it! You&#8217;re all done. You now have a robust and secure login system for your app that validates users&#8217; email addresses.
</p>
]]></content:encoded>
			<wfw:commentRss>http://technologyvoodoo.com/articles/user-registration-with-email-confirmation-in-ruby-on-rails/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.345 seconds -->
