Santiago Hirschfeld
2006-Jan-20 01:20 UTC
[Rails] 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 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>
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 ! On 1/19/06, Santiago Hirschfeld <jsantiagoh@gmail.com> wrote:> > 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> > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Roberto Saccon - http://rsaccon.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060120/5661d33c/attachment-0001.html