Search code examples
ruby-on-railsrubymechanizemechanize-ruby

Ruby-Mechanize: undefined method `button_with'


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>

Solution

  • 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.