Form_tag and multiple buttons inside problem

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
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 


<tr> <th></th>
<% for p in @product.pieces %>
    <td><%= button_to 'Edit Piece', :action => 'edit_piece', :id

=> p %>

<%=h p.nombre %>
<%=h p.descripcion %>
<%= button_to ‘Delete Piece’, :action => ‘delete_piece’,
:id => p %>

<% end%>
<%= 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:

blah blah blah HTML code here ....
Piece Name Piece Descrip

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
better: protoitype.js) has currently some known limitations for that.

Anyway, for non-ajax multiple submit buttons within one form, you just
several buttons into one form (in your code you are nesting forms, that
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
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
your friend !

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs