Forum: Ruby on Rails form_tag and multiple buttons inside problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A2bfa8f483e1aa2c7e4188d1d30af778?d=identicon&s=25 Santiago Hirschfeld (Guest)
on 2006-01-20 02:58
(Received via mailing list)
Hi, I have a bit of a problem with a form in rails. Well, the problem
really is
me not fully understanding the mechanism of the form_tag, so here's what
i try
to do:

The idea is to present a some edit fields for some @product attributes
and then
present a table showing the @product.pieces  (pieces for every @product)
and to
include a "Edit Piece" and "Delete Piece" button in every row of the
table.
There's suppose to be a "Edit Product" and "Create New Piece" at the
end of the window.

The problem is that (as shown below) the first button does the :action
=>
'update' of the form, so doesn't call update_piece as it should. Maybe
I'm not
being very clear, please excuse my english is not my first language.

Here's the code for the view.


<%= start_form_tag :action => 'update',:id => @product %>

    <label for="product_nombre">Nombre</label>
    <%= text_field 'product', 'nombre'  %>

    <label for="product_descripcion">Descripcion</label>
    <%= text_area 'product', 'descripcion', :rows=>4  %>

    <label for="product_precio">Precio</label>
    <%= text_field 'product', 'precio', 'size' => 8, 'maxlength' => 8
%>

    <table>
    <tr> <th></th>
        <th>Nombre</th>
        <th>Descripcion</th>
    </tr>
    <% for p in @product.pieces %>
    <tr>
        <td><%= button_to 'Edit Piece', :action => 'edit_piece', :id
=> p %></td>
        <td><%=h p.nombre %></td>
        <td><%=h p.descripcion %></td>
        <td><%= button_to 'Delete Piece', :action => 'delete_piece',
:id => p %></td>
    </tr>
    <% end%>
    </table>

    <%= button_to 'Edit Product', :action => 'edit', :id => @product -%>
    <%= button_to 'Create New Piece', :action => 'add_piece', :id =>
@product -%>

<%= end_form_tag %>

what I get for the "Edit" button in the generated HTML is the following:


</form action="/admin/update/3" method="post">

    blah blah blah HTML code here ....

   <td><div><input value="Editar" type="submit"></div>	</td>

   <td> Piece Name</td>
  <td> Piece Descrip </td>
  <td><form method="post" action="/admin/delete_piece/11"
class="button-to"><div><input value="Eliminar"
type="submit"></div></form> </td>
30269682335f1fb247d71969fa715b5e?d=identicon&s=25 Roberto Saccon (rsaccon)
on 2006-01-20 07:22
(Received via mailing list)
I don't not fully understand what you want to do, but if you wanna have
multiple submit buttons then you need to do some workarounds, becaue RoR
(or
better: protoitype.js) has currently some known limitations for that.

Anyway, for non-ajax multiple submit buttons within one form, you just
put
several buttons into one form (in your code you are nesting forms, that
does
not work, I think), and in the controller you check which button was
pressed. For ajax, you need to add a hidden field to the form, give it a
default value and change that value with javascript when the second
submit
button was pressed, because serialization in prototype  does not support
multiple submit buttons.

There are code examples for all of this, floating around the web, google
is
your friend !
This topic is locked and can not be replied to.