Ruby for Programmers – Meta 1: HTML

Just in case you’re following from the blog, the next few parts will involve HTML rendering. Since these lessons are about Ruby / Rails, I’m going to assume that you know how to write HTML by hand, since we’ll be introducing a lot of magic functions that create HTML for you. If you don’t know how to HTML, Shay Howe wrote a wonderful HTML tutorial, which is available on his website.

Shay’s tutorial goes into box models and typography early on, which is important for creating custom pages. But, I will be using Bootstrap as the default CSS theme for this tutorial. So instead of writing my own box models and typography CSS, I’ll be using Bootstrap’s CSS and HTML prescriptions.

Ruby for Programmers – Part 11: Railtying It Together

Having all this on one script is okay for now, but it’s best to start splitting up files just to make it easier to follow. So let’s create some folder conventions for us:

  • app/controllers will contain our controllers as determined by our routes
  • config/routes.rb will contain the routes drawer
  • config/boot.rb will contain the boot script
  • config/application.rb will contain information specific to our application, and hold information between files.
  • config/environment.rb will have the initializers after our application.
  • public/ will contain public files like CSS or error files

So first in boot.rb we’ll put the require ‘bundler/setup’. Since it’s in another folder, we’re going to need to tell that the file is in another castle:

ENV is a sweet built-in Ruby variable that contains the shell environment variables. This is useful if you want to store different configurations for different setups. Instead of different that may or may not be uploaded, it’s far better to store this information in an environment variable. You can setup this on the server so as to hide passwords or API keys too.

Below that is a really short version of the Rails application initializer. So we created a Rails module (for now think of modules as function containers), and made it so that Rails.application is accessible from anywhere. We then created a class named Application and used a Ruby auto-function named “inherited” where if a new class is made, it will run that function. In this case it sets Rails.application to a new instance of the child class. (If you’re confused, don’t worry, this is all automagicked away soon).

We also create the routes and the routes file:

Which is just a copy/paste of our original routes. Note that it uses our Rails.application that was created, which means we need to create the child class to be inherited and instanced:

Pretty boring here except this is where we create the class to be inherited. It also requires the boot file and mainpage_controller.rb file here too. mainpage_controller.rb itself is just a copy/paste:

Exactly like the old one.

For now, our environment.rb will just include the application and the routes file:

And will just include the environment.rb and run the routes:

This boots and shows the same stuff as before with our routes.

You can view the working example here:

Now before I go, there’s a reason why all this moving around, it’s just to show how Rails ties things together. In fact, the gem that holds all this together is “railties”. So let’s add it to our Gemfile, and update our scripts to use it:

And run bundle install. Now we only need to make a few small changes to a few files. boot.rb will get it’s Rails stuff removed since it’s now handled in the railties gem:

environment.rb now tells Rails to initialize (which will auto-load important files like routes.rb):

And will run Rails.application instead of the routes, since Rails will handle some automagic for us:

Since railties will automatically load the controller files for us, we just need to make a couple small modifications to our application.rb file:

We tell it to look for Rails and ActionController (which is next lesson, but railties/routing was whiny without it). Also, we started on the config because Rails complained without it.

And it works. bundle exec rackup will boot your app and it will still work like before, but now running on Rails. Once you load, you’ll notice it whining about eager_loading, so lets add an environment file config/environments/development.rb:

You can ignore the actual settings until we need to go for them. However, this file is important because it defines some of the eager loading that makes Rails great. We’ll also add the default Rakefile too, while we’re at it.

Now you can run the app a different way bundle exec rails server. This boots the server a different way, but has all trimmings. rackup still works, but the rails server has better logging and debug files.

So this lesson just really moved things around, which is nice. But the controllers are still using the weird array output and a bad way to create HTML. So next lesson, let’s add some ActionControllers with some ActionViews.

The final code for this part is here:

Printing Raw EPL on the LP2844 from a Mac or Linux

This is mostly a list for my own purposes, but I got a Eltron/Zebra LP2844 to print raw files generated on my Mac to send over USB.

First, you need to find the device URI:

Add the printer to CUPS, accept, and enable it:

(cupsaccept is for Mac, “accept” works on all other flavors of UNIX.)

Try a test:


If you are just on Mac, you can actually add it through the System Preferences. Add the EPL printer via Print & Scan, click the plus to add the printer, click on the printer, and search and select EPL2 as the driver.

To print raw EPL commands, first find the command line name of the printer. List all printers via this command:

If Zebra_LP2844 was the printer you just added, then use this command to print:

Textile and the Starmen.Net Forums

As some of you might know, I — almost religiously — push Textile onto everybody on the Starmen.Net forums. I really like it since it makes a lot of organizational posts easy to do with all the block-level elements. Allowing inline styles is awesome too. Except that I highly parse the styles for certain elements now. Sarsie found all kinds of wonderful XSS attacks with CSS Expressions and IE6. I keep telling the story about a security problem with font colors.

Anyway, a lot of people have problems with the newlines and how they work. I’ve tried all kinds of things, but none of them work well really. Since I used RedCloth 3, the Ruby parser for Textile, there wasn’t a nice answer. Going without it is impossible, since there’s a legitimate reason to have single breaks. Enabling the official hard breaks on RedCloth 3 caused havoc on quotes and spoiler matocode tags. I eventually settled on an ugly hack.

In comes RedCloth 4. It fixes the line break issue, it’s a C-based extension for extra speed. And it doesn’t have disablers, it’s all or nothing. I have to disable images on the forum because it’s possible to display tasteless images. Not to mention the occasional Goatse hotlink protection. Stuff I really don’t want to be shown on a forum that has members of 12 or less.

However, We offer regular users the ability to post images in certain forums, like the Fan Forum, to show off their artwork. Administrators and moderators like images for various real needs, as well as special permissions allow certain users to post images for various needs. (ie. the runner of the currently running Mafia game.) So the feature disabler had to be easily disabled/enabled.

After a brief email in their mailing list, I manned up and added it myself. Unfortunately, I didn’t know Ragel, or Ruby C Extensions for that matter. It was a long and tough battle, but I defeated the final boss!"!image.jpg!",[:disable_inline=>:image]).to_html

Hopefully — With the feature disabler that I needed — this fixes a lot of problems that have been plaguing the Textile implementation on the forum.

The next step is to port the crappy MatoCode (my implementation of BBCode) to the fancy Ragel system. Things like the [quote] and [spoiler] tags need to have their insides parsed separately from the outside.

What I had to do to resize my Boot Camp Partition:

I heard iPartition was great, so I went to try it. Here’s what I did:

  • Needed a DVD, so I looked forever to find one.
  • Remembered that blank DVDs where in my car, went and got one.
  • Burned the iPartition boot DVD.
  • It didn’t work.
  • Burned it again.
  • Didn’t work.
  • I realized it might not have drivers for my new iMac, so I tried copying my /System/Library/Extentions over to the DVD, burned another one.
  • It didn’t work either.
  • I realized I could use my iBook instead while my iMac was in target disc mode.
  • Except my iBook would let me put in the DVD I burned. There appeared to be something jamming it. (WTF?)
  • So I tried a credit card to unblock it, no dice.
  • I had to open up my iBook, but I needed a smallish Allen wrench to open it. I looked everywhere.
  • I realized that it too, was in my car, so I went to go get it.
  • I took my iBook apart.
  • I took the Combo Drive apart. The disc let me put it in, no clue why. I guess it wanted some fresh air inside?
  • Put the iBook half-way back together, except I forgot to connect the Combo Drive up again.
  • Took it apart, hooked up the Combo Drive, and then put the iBook back together again.
  • Booted the iBook with the iPartition boot disc. (It worked fine.)
All-in-all iPartition is a pretty nice program to resize your Boot Camp partition. Getting to the program was a total pain in the ass.