0

My ruby script is throwing an error : undefined method `button_with'. The ruby version is 1.9.3-p484 and the Mechanize version is 2.5.1. I checked Mechanize help and v2.5.1 is compatible with Ruby v1.9.3.

form = a.page.form_with(:id => "form-name")
button = form.button_with(:value => "Submit Request")
a.submit(form, button)

a - is the Mechanize agent I initialized earlier

Could someone guide me as to what may be the issue or how to debug it?

Thank you!

<form id="form-name" action="/xyz_ajax" method="POST" onsubmit="return false;">
    <div class="form-footer">
          <button class="button button-size-default button-default" type="submit" onclick=";return true;" id="export-csv"><span class="button-content">Export report</span></button>
      <span class="csv-status" id="status-text">
          &nbsp;CSV export is finished.
      </span>
       &nbsp;<a id="download-url" href="/url/sample/abc.htm">Submit Request</a>
    </div>
    <input type="hidden" name="session_token" value="abcde1234">
      <input type="hidden" name="csv_export" value="1">
      <input type="hidden" name="report" value="True">
  </form>

1 Answers1

0

Does it say NoMethodError: undefined method 'button_with' for nil:NilClass?

It means form is nil because form_with(:id => "form-name") couldn't find your form.

aidan
  • 9,310
  • 8
  • 68
  • 82
  • Thanks for the quick reply, Aidan. The complete error message is as below "Exception caught: NoMethodError. Exiting. Message: undefined method `button_with' for nil:NilClass" – Anoorag Saxena Jul 19 '17 at 06:20
  • Yep, sounds like `form_with()` is returning `nil` instead of the form you're looking for. – aidan Jul 19 '17 at 06:21
  • I see what you are saying - but I am looking at the website and I see the form with the name "form-name". What could I be missing? – Anoorag Saxena Jul 19 '17 at 06:21
  • @user3588980 -- Did you say that the name of the form is `form-name` but you're finding it with `id`? – 31piy Jul 19 '17 at 06:29
  • My bad in using the wrong jargon. The id of the form is "form-name". – Anoorag Saxena Jul 19 '17 at 06:30
  • Is the ID definitely on the `form` element, and not something like a parent `div`? No iframes are involved? Maybe run irb or pry and try the lines of code one-by-one. Or dump out the contents of `page.form` to try and get an idea of what's going on. – aidan Jul 19 '17 at 06:31
  • @aidan I updated my original post with the HTML for the form. – Anoorag Saxena Jul 19 '17 at 06:37
  • The HTML looks fine to me. Without any more information I think you're on your own. Try `a.page.forms.count` to see if mechanize is finding _any_ forms. – aidan Jul 19 '17 at 06:48
  • Cool! Thanks so much for all the help, @aidan. I will try taking the dump of page.form and doing the forms.count and see whats going on. – Anoorag Saxena Jul 19 '17 at 06:59
  • Good luck! If you can, use `irb` (or even better, `pry`), it should help you investigate the problem faster. – aidan Jul 19 '17 at 07:05