1

I was looking through this response to having different paths to a second submit button How do I create multiple submit buttons for the same form in Rails?, and ended up using formaction: my_path to specify the new path.

This works nicely if the form has the same method i.e. POST, but doesn't seem to work if the form requires a different method.

I was wondering whether anyone knew a neat solution like the formaction: my_path solution or a rails helper or if in fact the best way forward is to reset the method via JS.

Christian Bell
  • 314
  • 2
  • 11
  • You could re-create the get/post/put/patch request from within the controller action itself, depending on whether button 'A' or 'B" was clicked. That's one possible solution and probably the easiest, the other would be to use js to dynamically add in the path required on the button click of the form. But why do you want to use two different methods on the form? that's what's a little confusing. – BenKoshy Dec 10 '18 at 18:10
  • So essentially the first method is a POST request - this references a calculate method in the controller that checks the inputed value against a `calculate_method`. This is then displayed via AJAX to the user as it has an effect on other values, if the user is happy then he clicks 'confirm calculation' and the existing value is updated via a PATCH request to an update method – Christian Bell Dec 11 '18 at 09:47
  • if that's the case then another option would be the same form, but for two entirely different actions: one for POST, and the other for PATCH. But you would have to set the URL of the form and the method (i.e. POST or PATCH), with an if statement in the `form_with` or `form_form` rails helper method. just msg if that doesn't make any sense and if it's of interest to you. – BenKoshy Dec 11 '18 at 17:49

1 Answers1

1

Managed to find out what it was for those who are interested, there is an html element 'formmethod' that you can set to give the request a different method.

Here's a non-rails example from W3 schools - https://www.w3schools.com/html/html_form_attributes.asp

<form action="/action_page.php" method="get"> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br> <input type="submit" value="Submit"> <input type="submit" formmethod="post" value="Submit using POST"> </form>

Christian Bell
  • 314
  • 2
  • 11