Archive for January, 2009

Why I’m using Skribit

If you haven’t noticed yet I have added a new item in the right sidebar called Skribit. Skribit essentially is a blog topic suggestion application developed by Paul Stamatiou. Paul is a recent graduate of Georgia Tech and is now working on Skribit full time. If you haven’t checked out his blog yet, I’d definitely suggest reading it. Plenty of good inspiration and great posts over there.

I hope to use Skribit as a way to communicate with my readers directly about the topics I write about. I mostly plan on writing tutorials about using CSS and Ruby on Rails to develop web applications and with Skribit now all of my readers will be able to suggest topics for me to write about. Read more

Tooltips in CSS

A tooltip is a small box that appear near an object in a graphical user interface when a pointer or other cursor controlled by a mouse passes over or rests on that object and which contains a brief text message identifying or explaining the object. In the context of a website the information shown in a tooltip is read from the title tags of an element on a page. These are stylized all over the web general using JavaScript and CSS. That’s not what I’m going to be teaching you to create though. I am going to show you how to write a tooltip in pure CSS.

Let’s start with a basic XHTML document.

<p>
    <a href="http://www.johndesu.com/" class="tooltip">
    John Dyer<span> (Visit my blog) </span></a>
    is a student at the University of Pittsburgh majoring in computer science.
</p>

Notice that the link has been given a class of tooltip. Inside my link I have added the text I want to be displayed as the link text and the text I want to be displayed in a tooltip. It is good practice to enclose the tooltip text in brackets like I have done so that it will still make sense if styles are turned off.

Now we need to set the position property of the anchor tag to relative in order to be able to position the contents of the span absolutely in relation to it’s parent element, the anchor tag. We also don’t want the tooltip text to display by default so we will set it’s display property to none.

a.tooltip {
    position: relative;
}
 
a.tooltip span {
    display: none;
}

Next we want to make the text contained in the span tag appear when the anchor is hovered over. We do this by setting the display property of tooltip:hover span to block and positioning the contents of the span below and to the right of anchor using absolute positioning.

a.tooltip:hover span {
    display: block;
    position: absolute;
    top: 1em;
    left: 2em;
}

And that’s really all there is to it. Let’s add some custom styling now to make it look more like a tooltip. Feel free to change any of stylistic settings to your liking. This is obviously just an example.

a.tooltip:hover span {
    display: block;
    position: absolute;
    top: 1em;
    left: 2em;
    border: 1px solid #996633;
    padding: 0.3em 0.5em;
    background-color: #FFFF66;
    color: #000;
}

Note that this technique requires a standards compliant browser such as Firefox. Of course there are issues in IE and for some reason there can be problems in Safari as well. A quick fix for IE 5.x from Andy Budd is the following:

a.tooltip:hover {
    font-size: 100%; /* Fixes bug in IE5.x/Win */
}

View a working example

If you enjoyed this tutorial, why not subscribe to my feed to receive the latest updates and tutorials from JohnDesu.

Rails Envy – Ruby on Rails vs. Django Commercial

This is an oldie, but a goodie. I recently saw this posted on Reddit and thought it was funny enough that I would share it here as well. In 2007 at the Lone Star Ruby Conference, the ever hilarious guys over at Rails Envy premiered a commercial they made imitating the Mac vs. PC commercials from Apple. I would try to give some sort of description of the video, but I think it’s better that you just watch it.

Warning! Mega-Nerd Alert!

Although this video is clearly seeming to make fun of Senor DJ Angoooo, Gregg wanted to point out that he thinks Django is actually a great framework for building web applications.

After taking a good look at Django and weighing all the pros/cons, I didn’t really think we should make fun of it. Django is a great framework for building web applications, one that employs many of the same techniques that Ruby on Rails does. If it wasn’t for Rails I’d probably be programming Django right now. Amongst a sea of mediocre web frameworks it’s definitely close to the top.

Original Source

Getting Started With Rails Migrations Part 1: Creating Migrations

A migration is simply a Ruby source file in your application’s db/migrate directory. These files help to make small changes to the database extremely easy and quick. Want to add a table or rename a column? Write another migration and run rake db:migrate. With automatic versioning you are able to see the changes made to the model just by looking at the latest migration file. No more repetitive SQL queries. Now you can access your data in an understandable way right in your code. With that, let’s get coding!

First we can use either the rails generator to create a migration or use the model generator, which will also give us a migration file. Note: you could create these files by hand, but it is less error-prone and probably more efficient to use a built-in generator.

To create a migration with the model generator we would do something like this:

ruby script/generate model post
  exists app/models/
  exists test/unit/
  exists test/fixtures/
  create app/models/post.rb
  create test/unit/post_test.rb
  create test/fixtures/posts.yml
  create db/migrate
  create db/migrate/20090113055719_create_posts.rb

To create a migration on its own we would do something similar to this:

ruby script/generate migration add_category_column
exists db/migrate
create db/migrate/20090113055952_add_category_column.rb

To run the migration we use the db:migrate task

rake db:migrate

When running the db:migrate Rake task, a schema_migrations table is looked for to keep track of migration versions. If one isn’t found it will be created on the fly. After each migration is run a row in the schema_migration table is added.

You can also use the db:migrate Rake task to force the database to a specific version by supplying it with the Version= parameter.

rake db:migrate VERSION=20090113055719

To revert the database to it’s original state supply Version= paramter with 0.

rake db:migrate VERSION=0

If you run a migration whose version is higher than the version of the current database, the migration will be applied. Somewhat differently, if you run a migration whose version is lower than the version of the current database, Rails will look for the migration file whose number matches the database version and undo it.

I hope this tutorial has helped you to begin to understand ActiveRecord and how useful migrations can be. In the next article in this series we look at what makes up a migration in more detail.

Getting Started With Ruby on Rails: Resources (Paid and Free)

I have gone through all of my Ruby on Rails resources and have come up with a list of what I think are the best resources to get you started with your Ruby on Rails journey. This will be the first of a few lists about Ruby on Rails I will be posting over the next week or two. Following the getting started list will be a list on intermediate resources, advanced resources, and finally a list of some commonly plugins and blogs that I really think will help boost your understanding of Ruby on Rails. So with that, let’s get started!

Free Resources

  1. First you’ll probably want to get Ruby on Rails installed on your machine. To do this is quite simple and this website will help you along the way. If you are on a relatively new mac, you probably already have Rails installed and therefore just running the command ‘gem install rails’ from the terminal will get you the newest version of rails and all of it’s dependencies. More on this on the official Ruby on Rails website.
  2. Rolling With Ruby on Rails Part 1 and Part 2 – This is a slightly outdated tutorial as it was written in 2005, but it still serves as a great introduction to the language of ruby and the framework rails provides. 
  3. Really Getting Started with Rails – Amy Hoy over at Slash7 has put together a great guide that not only covers the the basics of RoR, but also introduces the reader to the basic concepts of the Ruby scripting language.
  4. Getting Started With Ruby on Rails – The excellent A List Apart wrote an article not very long ago that shows the benefits of using RoR for a web project. It introduces the reader to the basics of ruby and many concepts of the rails MVC framework.
  5. Using Ruby on Rails for Web Development on Mac OSX – Apple has written an article on getting started using ruby on rails with a fresh install of Mac OS X. It is a great tutorial that takes you through the creation of a basic RoR web application.
  6. Ruby on Rails from Scratch – Probably my favorite of all the free tutorials I have listed. This is a four part series of very visual tutorials for learning RoR. I know most people are visual learners, so this series is great for you. In fact, parts of the series are video tutorials. Definitely worth checking out.
  7. Rails Framework Documentation – This is the Rails API. Get familiar with it because it will become extremely useful the more you learn to love it.

Read more