'Why f.submit is not working when I try to use it?
I created a form to my User database change his password, as it follows :
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<div class="container">
<form action="/action_page.php">
<%= f.label :password, "Nova senha" %><br />
<% if @minimum_password_length %>
<em>(Mínimo de <%= @minimum_password_length %> caracteres)</em><br />
<% end %>
<%= f.password_field :password, autofocus: true, autocomplete: "off" %>
</div>
<div class="container">
<%= f.label :password_confirmation, "Confirmar nova senha" %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</form>
</div>
<div class="actions">
<%= f.submit "Alterar minha senha" %>
</div>
<% end %>
The problem about it is that my f.submit "Alterar minha senha" is not working, it doesn't receive the action of my click. How can I fix it ?
Solution 1:[1]
You now have 2 forms, one created by form_for() and then you difine another form in your html the button is not in the html form so it wont submit those fields. Either remove the tag from your html or move the button within the form tag
Solution 2:[2]
The problem is likely that nested forms are not allowed in HTML. You have defined two separate forms, one inside the other. The first is generated by your call to form_for
. The second is defined by your <form>
tag. Thus, what is probably happening is that your submit button is also submitting the fields on the inner form.
By looking at your code, it appears that you don't even need the inner form—you use the FormBuilder f
object from form_for
to generate your inner form. However, either way, you should consider re-arranging your forms so that they are not nested.
Also, on a side note, your HTML is invalid. Your closing </form>
tag comes before the closing </div>
tag for .container
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nico Shultz |
Solution 2 |