Basic tech stuff

Programming and Linux administration

Archive for May, 2007

Rails fix for nested forms

Posted by Daniel Brahneborg on 2007 May 26

In Rails there is a nice helper function button_to. It uses Javascript in the onclick event to create a new form which is immediately submitted. Perfect when a button is all you really need.

Or so I thought, until I installed the HTML Validator extension to Firefox. It has a super strict HTML parser that tells you when something is wrong with your code. This way there is a much greater chance that the pages will look the way you want in as many browsers as possible.

So what’s the problem? Well, I had a form surrounding a table. The form was really only for the last row in the table, but since a form must either be outside of the table tag or within a td, the only option was to let the form surround the table. On all rows but the one with the real form fields, I wanted that new button. When the new form was created, the HTML validator became a bit upset about the fact that a new form was created within an existing form. This isn’t allowed. (There are sure a lot of things that aren’t allowed in HTML.)

The solution was to patch actionpack/lib/action_view/helpers/url_helper.rb around line 367, changing

    "this.body.appendChild(f); f.method = 'POST'; f.action = this.href;"


    "document.body.appendChild(f); f.method = 'POST'; f.action = this.href;"

This makes the new form live as a child to the top level body tag instead. Now the validator was happy again.

Posted in programming, rails | Leave a Comment »

Reasons for automatic tests

Posted by Daniel Brahneborg on 2007 May 22

As everybody should know by now, there are plenty of reasons for writing automatic tests. One of them, of course, being the fact that you get to see whether your application actually works, at least for some cases. That’s just a small part of the story, though.

Reason number two is design. To be able to write both small unit tests, large system tests and everything in between, the application simply must be well designed. Otherwise it will be impossible to test one thing at a time, and mock out the parts that should be faked. Knowing where to draw the lines between the modules in a system can be difficult, but by simply trying to write nice tests for them, it becomes much easier.

The third reason is refactoring, which I personally got bitten by this weekend when making a change to my RSS/Ping service, written in Ruby on Rails. In the first versions the “ping” logic was a separate program, but the users wanted it merged with the web application. So, I very carefully moved one function at a time into the classes where they belonged, and made a little button in the web interface. Despite being exactly the same code, with no uninitialized variables or anything like that, it simply refused to work.

Today I found the problem. The standalone program used a couple of require statements to import modules for RSS and Atom parsing and whatnot, things that weren’t used in the web application. This made the function fail, even though everything was technically fully correct. The standalone program worked, so tests wasn’t really necessary, I thought.

Lesson learned? Automatic tests are good, and should aim for full code coverage. This makes the programming part so much easier.

Andra bloggar om: , , .

Posted in programming, rails, testing | Leave a Comment »

Rails: belongs_to :polymorphic and inheritance

Posted by Daniel Brahneborg on 2007 May 10

The flag :polymorphic option for belongs_to associations is extremely useful, especially when implementing an authorization system. Let’s say that I have a Permission class that points to either a House or a Car:

class Permission < ActiveRecord::Base; belongs_to :authobject, :polymorphic => true; end
class House < ActiveRecord::Base; has_many :permissions, :as => 'authobject'; end
class Car < ActiveRecord::Base; has_many :permissions, :as => 'authobject'; end

This works just fine, making it possible both to find the object that a Permission is for, and the relevant Permissions for a certain object. In the column authobject_type you get the strings “House” and “Car”, respectively.

Now the problem: We want to replace the Car class with an abstract Vehicle class, with subclasses Car and Boat:

class Vehicle < ActiveRecord::Base; has_many :permissions, :as => 'authobject'; end
class Car < Vehicle; end
class Boat < Vehicle; end

In Rails version 1.2.3 this puts the string “Vehicle” in the Permissions.authobject_type column, which causes lots of stuff to fail. In my case, a bunch of test cases that simply created a Permission and made sure it could be found again. Suddenly it couldn’t.

The problem was this bug:, with a patch that makes sure that the real class name in the authobject_type field. The Rails documentation says you should store the base class in the authobject_type field, but that simply isn’t right, since it makes it impossible to load the right subclass. Since we still want to store permissions to Houses, it’s important that we store the exact class name.

Edit 2007-05-16: The problem is still not completely solved, since :dependent => :destroy still uses the base class name instead of the correct one. Since following the relationship works, you have to loop and destroy the objects manually.

Andra bloggar om: , , .

Posted in programming, rails | Leave a Comment »

This is NOT the HD-DVD key

Posted by Daniel Brahneborg on 2007 May 2

Just as normal DVD’s are encrypted, so are HD-DVD’s. In the latter case both a global key and a bunch of player specific keys are used. First the latter ones were found for a particular player, and a while ago the global key was found. Using this, and some software, any HD-DVD can be played and copied to a hard drive for later viewing.

Today Slashdot has an article about the censoring of this number. Writing it seems illegal, but I ought to be able to write that it is NOT f6 06 ee fd 62 8b 1c a4 27 be a9 3a 9c a9 77 3f. (Thanks to AJVM for the tip.)

Damn, the movie industry is stupid.

Andra bloggar om: , .

Posted in encryption | Leave a Comment »