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
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, > MatWell 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 ---
On Thu, Nov 02, 2006 at 03:41:18PM -0500, Mat Schaffer wrote:> 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.Thanks! I''ll apply this patch and it will be in the next release. --Aaron -- Aaron Patterson http://tenderlovemaking.com/
Hey Mat, On Thu, Nov 02, 2006 at 03:41:18PM -0500, Mat Schaffer wrote:> 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 ---I was thinking about this patch for a little bit, and I think it might be a little dangerous to add a field without the user explicitly knowing. I don''t see the Form#field method as a destructive method. Would you be okay with having an "add_field!" method, and also having the Form#[]method be destructive? That would make the Form class more Hash like. Basically these two code snippets would be exactly the same: form.add_field!(''name'', ''Aaron'') #The second arg could be nil -or- form[''name''] = ''Aaron'' Both of those would create a new field. Let me know what you think. --Aaron -- Aaron Patterson http://tenderlovemaking.com/
Aaron Patterson wrote:> I was thinking about this patch for a little bit, and I think it might > be a little dangerous to add a field without the user explicitly > knowing.I agree 100%. Had a partially composed email saying the same thing. Your recommendations are good. Jon
On Nov 3, 2006, at 1:22 PM, Aaron Patterson wrote:> I was thinking about this patch for a little bit, and I think it might > be a little dangerous to add a field without the user explicitly > knowing. > > I don''t see the Form#field method as a destructive method. Would > you be > okay with having an "add_field!" method, and also having the Form#[]> method be destructive? That would make the Form class more Hash like. > > Basically these two code snippets would be exactly the same: > > form.add_field!(''name'', ''Aaron'') #The second arg could be nil > > -or- > > form[''name''] = ''Aaron'' > > Both of those would create a new field. Let me know what you think. > > --AaronYeah, that''s a really good point. I only needed statements like form [''name''] = ''Aaron'' work, and adding to the Form#field method seemed to be the simplest way to achieve that. Since the form is accessed like a Hash, using Form#[]= makes sense to me. Probably the most intuitive and it doesn''t add another method to the interface. add_field! isn''t bad either though, if that''s your fancy. Good catch, thanks, Mat