Archive for August, 2005

@model = SomeType

Tonight I was working on my bicycling log in Rails. I have a bunch of simple attribute tables that consist of just id and description fields, so I wanted to factor out as much common code as possible. I made common views for listing, editing and creating (no deleting or viewing) easily enough. When I got to the controller code, I wanted to have generic code for the actions, but got stuck on how to assign a type name to a variable and then get the class for the name. I was thinking in Java, not Ruby, so I kept looking for a “class_for_name” name method. After about ten minutes I thought, “duh, everything is an object in Ruby!

@model = SomeType
@items = @model.find_all
# or
@item = @model.new(params[:item])

Joy.

Advertisements

Pages

Categories

Copyright ©2008-2013 James E Orchard-Hays, All Rights Reserved