=button_to does not generates form if already in an other form
-
11-09-2019 - |
Question
Using the same form partial in both create and edit in my case new and preview. partial looks somewhat like this (I use HAML)
=form_tag ({:action => params[:action]}, :multipart => true)
=text_field :newsletter, :title
=text_area :newsletter, :body
=file_field :newsletter,:attachment
-if params[:action] == "preview"
=button_to "select contacts and send", :action => "contacts"
=submit_tag "save and preview"
but in the html-output is
...
<input type="submit" value="select contacts and send"/>
<input type="hidden" value="rwYnZlEpWV4dR89zjgprEALBYmP0xqM3lnKt9JDLyak=" name="authenticity_token"/>
<input type="submit" value="save and preview" name="commit"/>
...
why does the button_to not generate the button-to form?
a solution is to keep the button outside the partial and only 1 per form, but how can I have 2 buttons in the same form?
edit: another workaround would be a hidden checkbox that is set by javascript if the button_to is pressed and submits the form, separating them in the controller
Solution
HTML doesn't actually allow for forms to be nested. Some browsers do, but you will see some crazy behavior in others. Is the HTML you are showing the raw output or the computed DOM from firebug?
The preferred way to handle this case is to move your button_to
outside the other form. If you're cool with JS-only options, you can use link_to :method => :post
and style the link to look like a button.
OTHER TIPS
In the end used the
:name => 'otheraction'
in the controller then checked if either
params[:commit] or params[:otheraction]
was in the params hash
I build a gem called button_link_to
.
Using the button_link_to method will generate button tag but without nested by form tag.
Just using it like link_to.
You can get more information from here: https://github.com/tonilin/button_link_to
If you want see a demo: http://button-link-to.herokuapp.com/