December 19, 2008

Rails vs PHP

Filed under: Random Business Thoughts — Paul @ 5:51 pm

Though you hear it all the time, comparing Ruby on Rails to PHP really is not fair - they are different animals entirely, only sharing the goal of creating websites that do stuff.

Rails, being a framework, could be more aptly compared to PHP frameworks, such as CakePHP, or Zend View. But even then they are still very different - and the difference is Ruby.

Ruby is beautiful, elegant and easy to read. PHP is pragmatic and direct but disorganized (imho).To Ruby, everything is an object, which serves as a good organizational mechanism. In PHP everything is a global function, usually with funky names that give little indication to what it does. It is like the difference between an organized toolbox, with all the right tools in all the right places and one big box with all the tools thrown in haphazardly.

Let’s look at a overly simplified example of some Ruby code, often used in Rails.

1.day.ago

What do you think that means?

Let’s look at it in PHP.

time() - 86400;

Hmm, I think most of us could figure out the PHP but anyone could read the Ruby. Which brings up another subject. The Ruby code above is actually an extension of a core class/object in Ruby, which Rails does - a lot, to great effect.

There are many more/better reasons for choosing Ruby and Rails, of course. But, for me, it is the simple things that Ruby allows which makes Rails … different and better.

Use CSS to Mark Fields as Required

Filed under: TDS Developer's Corner — Nick @ 5:17 pm

Curious on how the pros handle marking required fields on their forms?  Wonder no more!  Below I’ll describe a technique I employ on my own forms to make marking required fields a simple and straight-forward process.  I will assume you have at least a basic working understanding of CSS, but even if you do not you should be able to still employ this technique.

First lets start with the HTML:

<ol>
<li class="required">
<label for="item1">Item One</label>
<input name="item1" id="item1" value="" />
</li>
<li>
<label for="item2">Item Two</label>
<input name="item2" id="item2" value="" />
</li>
<li class="required">
<label for="item3">Item Three</label>
<input name="item3" id="item3" value="" />
</li>
<li>
<label for="item4">Item Four</label>
<input name="item4" id="item4" value="" />
</li>
</ol>

This is a standard ordered list.  You may be curious why I choose to wrap my form elements inside a list?  It’s a recommended best practice, as is the use of the for attribute in the label.  If you want to learn more you can google for “form fields li best practices” to read the opinions out there.

Now to style our two required fields with *’s is a simple matter of some CSS code.  Our goal is to not actually use text in the html itself.  The reason we don’t want to do this is that “required” is not really content, it’s meta-information.  Marking a field as required or not therefore is something that should be treated as a style, not content.

If a client came back and asked to have the mark for required fields be treated differently, say instead of a * (star) they wanted an ! (bang), we only have to edit one little line of code.  What if they didn’t want a mark at all but instead wanted to have it highlighted with a red box?  You start to see the wisdom in handling this through a style than in the html itself.  We’ll need just a handful of styles to accomplish our goal:

 ol { list-style: none; }
li.required:after {
content: "*";
font-size: 18pt;
margin-left: -5px;
}
label {
display: block;
font-size: 14pt;
align: right;
margin-right: 0.5em;
}
input {
margin: 5px;
font-size: 14pt;
}

The :after pseudo-selector on the li element is a special selector for a marker.  You can think of a marker as a sub-element of the primary element that appears before or after it (in this case the li element with the class of required).  We could also select the marker that appears before using :before.  The resulting web page will look as below: