7 Ton Shark is the personal weblog of Elliot Nelson.

Text

Let’s say you have a Rails model with a simple composed_of address.

class User < ActiveRecord::Base
  composed_of :address, :mapping => [%w(street street), %w(city city)]
end

class Address
  attr_reader :street, :city

  def initialize(street, city)
    @street, @city = street, city
  end
end

And a view that uses fields_for to pass in the field values.

<% form_for :user do |f| -%>
  <% f.fields_for :address do |ff| -%>
    <%= ff.text_field :street %>
    <%= ff.text_field :city %>
  <% end -%>
<% end -%>

Looks just like all of those examples online, right? After submitting your new form, though, you will receive the following error.

NoMethodError: undefined method `street' for {:street=>"123 Main Street", :city=>"Denver"}:HashWithIndifferentAccess

This is because out of the box, composed_of does not allow you to use your model this way. You need to create a custom converter, as shown in the following example.

class User < ActiveRecord::Base
  composed_of :address, :mapping => [%w(street street), %w(city city)],
    :converter => Proc.new {|args| Address.new(args[:street], args[:city])}
end

Now, when we submit our form, the User model knows exactly how to convert the “address” hash into an “Address” object.

Text

In IE7, put a paragraph in italics immediately inside a fieldset, and the fieldset top border is “copied” inside the paragraph, giving it a strike-through effect.

HTML:

<fieldset>
  <legend>Account Info</legend>
  <p class="instructions">Enter your account information below.</p>
</fieldset>

CSS:

fieldset {
  border:none;
  border-top:solid 1px #333;
  width:600px;
}

p.instructions {
  color:#333;
  font-style:italic;
}

Example:

IE7 Problem

The solution is to add a small left margin to the paragraph. This will (magically) remove the strike-through.

New CSS:

p.instructions {
  color:#333;
  font-style:italic;
  margin-left:4px;
}

Example:

IE7 Solution