ruby-on-railsruby-on-rails-6

Rails 6 Create new record and print to printer after comfirmation


I have this form_with that creates a new record Post. I am trying to open the print window when the user clicks on the submit button. If rails predicts it should be something like adding onconfirm:printpage() to the Submit button but I cant seem to find how to do it.

= form.submit data: { disable_with: false, confirm: "Are you sure?" onconfirm: printpage() }

and then:

function printpage(){
  window.print()
}

Solution

  • I think you can do just like this:-

    = form.submit, :class => "form_submit" ## add class to submit button
    
    :javascript
      $(document).ready(function () { 
        $("#submit").click(function(event) {
          if( !confirm('Are you sure that you want to submit the form') ){
            event.preventDefault();
           }else{
            window.print(); 
            $('#myForm').submit(); ## your form id
           }
        });  
      });