ruby-on-rails – Multiple submits by calling multiple methods in a single form

Question:

Hi! I have a form that contains two buttons, each button, when clicking, takes me to a different method within the same controller. How do I get what I need to do? Because I'm not succeeding.

form

<%= form_tag (@portabilidade), method: :get, id: "form-motivo" do %>   <div class="form-group">
    <strong>Motivo</strong>
    <%= text_area_tag :motivo, @portabilidade.motivo, class: 'form-control' %>   </div>   <%= button_tag "Liberar", class: "btn btn-primary", id: "btn-liberar" %>   <%= button_tag "Reter", class: "btn btn-info", id: "btn-reter" %> <% end %>

Javascript

  $("#btn-reter").on("click", function () {
      e.preventDefault();
      $('#form-motivo').attr('action', "/reter");
      $("#form-motivo").submit();
  });

  $("#btn-liberar").on("click", function () {
      e.preventDefault();
      $('#form-motivo').attr('action', "/liberar");
      $("#form-motivo").submit();
  });

I don't get any error, just the URL changes and doesn't record, nor enter any of the methods.

My URL was supposed to look like this:

/portabilidades/:id/reter ou .../liberar

It was like this and it didn't work:

portabilidades/61?utf8=✓&motivo=erer&button=

Where is my error? Appreciate!

Answer:

You can use formaction

<button type="submit" formaction="/reter">Reter</button>
<button type="submit" formaction="/liberar">Liberar</button>

Here it worked exceptionally 😀

Scroll to Top