Skip to main content

Rails Button_tag not submitting edit form, but submits new form

Rails • Asked by Alan Reid

I am using the following to submit my form when creating a product.

<%= button_tag("Create", type: "submit", form: 'new_product') %>

However, the issue I am facing is that when I try to do the same for editing a product, the form does not submit at all.

<%= button_tag("Create", type: "submit", form: 'edit_product') %>

My question is, what am I missing from this not to submit the edit form? I mean when I press the button, nothing happens at all. haha

I know there's bound to be simple answer thanks in advance


Hey Alan,

Are you using this button outside the <form> tags?

Check out https://stackoverflow.com/a/12567605

You'll need your form to have an ID and use that in your button form: 'form_id'

Looks like there may be some issues in IE with it though, so you may consider a JS solution if you need to support IE older than Edge.


Yeah its outside the form tag.
You made me look at it again, and the form is using the ID of the item!

So doing the following fixes the issue!

<%= button_tag("Create", type: "submit", form: 'edit_product_3') %>

Great!

You may consider setting a fixed ID to the form so its always the same. You may have issues if your form is now edit_product_4 but your button was hard coded to edit_product_3 unless you're dynamically setting the form: option as well to account for that.


yeah I set the form using form: 'edit_product_' + params[:id] cheers as always mate :D


Excellent!

Cheers!! :)


Oh neat, so you can have a button_to that submits a specific form?

I had always used it to have it submit a POST to a URL instead. Today I learned. 👍🧐


Yeah, I hated having it all in the form so start to mess around. I found the button_tag element could be attached to a form essentially so i can put the button anywhere on the page. It was just this edit issue i had which stumpped me haha.

So happy to get it all working now :D can finish this app off and move on to the next version :D


Aye, if you check the docs (under the options section) you'll see it will pass any additional options as regular tags which is really snazzy.

https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag

The biggest down side is IE compatability (who'd a thunk?) - so you'll still want a fallback solution if you need to support IE 11 or older. https://caniuse.com/#search=form


yeah its really cool, i will look to use it on some other bits too i think. It will be a nice solution.

IE haha, I am counting the days til its axed :D I wonder how MS will mess up Chrome though haha


They're supposed to start using Chromium in the future, so we might have browser compatibility after all in the future! how weird will that be??


fingers crossed they dont mess too much, could be a decent browser. I wish Google would reduce the resources used though, still a beast of a browser.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.