Wednesday, June 13, 2007

Changing form element names

Last night's facebook update is incompatible with the way the rails handles form parameters. "object[field_1]" becomes "object" => "Array" instead of "object" => {"field_1" => ...}. Until the issue is resolved, a simple fix is to tell rails to name forms using parentheses instead of brackets. Additionally, rails will need to parse parentheses in parameter keys as brackets. The following code does just that. To use, just slap it at the bottom of your environment.rb.

module ActionView
module Helpers
class InstanceTag
def tag_name
"#{@object_name}(#{@method_name})"
end

def tag_name_with_index(index)
"#{@object_name}(#{index})(#{@method_name})"
end
end
end
end

class CGIMethods
def self.parse_request_parameters(params)
parser = FormEncodedPairParser.new

params = params.dup
until params.empty?
for key, value in params
if key.blank?
params.delete key
elsif !key.include?('(') && !key.include?('[')
# much faster to test for the most common case first (GET)
# and avoid the call to build_deep_hash
parser.result[key] = get_typed_value(value[0])
params.delete key
elsif value.is_a?(Array)
parser.parse(key.tr('()', '[]'), get_typed_value(value.shift))
params.delete key if value.empty?
else
raise TypeError, "Expected array, found #{value.inspect}"
end
end
end
parser.result
end
end

2 comments:

mikee said...

I believe this only works in Rails 1.2 - is that correct?

Michael Niessner said...

I have only tried it in Rails 1.2. I wouldn't be surprised if it doesn't work in older versions.