I have a typical CRUD app, I would like to add a link next to the f.submit that allows you to go back to the index without saving changes. I thought it would be as simple as just making a link to the index, but it’s saving changes anyway.
<%= button_to 'Close Without Saving', users_path %>
<%= f.submit %>
What’s the rails way of handling this?
From the fine manual:
So using
button_toinside a form will attempt to create a nested form but HTML forms don’t nest. The result will be an HTML structure something like this:But the browser will ignore the inner
<form>and assume you meant this:Then pressing either
<input type="submit">will submit the outer form.You can either create another submit button in the outer form and the controller can check which button was pressed or you can use
link_toand style the link to match your submit button (or leave it styled like a link depending on your preference).