[Mechanize-users] Adding fields to a form

Mat Schaffer schapht at gmail.com
Thu Nov 2 15:41:18 EST 2006


On Nov 2, 2006, at 3:32 PM, Mat Schaffer wrote:
> Is there a decent way to add a field to a form before posting it?   
> I haven't tried using HPricot manipulations just yet, since I can't  
> ever find really solid docs on hpricot....
>
> Form#[]= doesn't work because it first searches only pre-existing  
> fields.  I'm investigating how to write  a patch now.
>
> But I thought maybe someone here might have an idea.
>
> Thanks in advance,
> Mat

Well I think I answered my own question.  Aaron, you deserve some  
props.  I haven't found a quirk in Mechanize yet that I couldn't  
patch in one line of code.  Way to be extensible.

--- field_patch.rb ---

class WWW::Mechanize::Form
   # Fetch the first field whose name is equal to field_name, or  
create a new field
   def field(field_name)
     fields.find { |f| f.name.eql? field_name } || fields <<  
WWW::Mechanize::Field.new(field_name, '')
   end
end

--- end field_patch.rb ---



More information about the Mechanize-users mailing list