Multiple submits calling multiple methods in a single form

Asked

Viewed 126 times

3

Hello! I have a form that contains two buttons, each button, when clicking, leads me to a different method within the same controller. How do I do what I need to do? Because I’m not getting it.

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 get no error, only the URL changes and does not save, nor enters any of the methods.

My URL that’s supposed to be:

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

It stayed that way and it didn’t work:

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

Where is my mistake? I thank you!

  • already tried to exchange :get for :post ?

  • Yes, I made several exchanges, but nothing took effect

1 answer

0


You can use the formaction

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

Here it worked exceptionally :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.