Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

=button_to does not generates form if already in an other form

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

like image 803
Jakob Cosoroaba Avatar asked Sep 19 '09 14:09

Jakob Cosoroaba


2 Answers

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.

like image 144
austinfromboston Avatar answered Oct 05 '22 22:10

austinfromboston


In the end used the

:name => 'otheraction'

in the controller then checked if either

params[:commit] or params[:otheraction]

was in the params hash

like image 35
Jakob Cosoroaba Avatar answered Oct 06 '22 00:10

Jakob Cosoroaba