1

Is it possible to create multifunction/nested forms with sinatra?

Right now I am doing something like this:

<form action="/section/<%= section.id %>" method="post">

  <input type="hidden" name="_method" value="put">
  <input name="heading" value="<%= section.heading>">
  <input type="submit" name="update" value="Update Section">

  <form action="/section/<%= section.id %>" method="post">
    <input type="hidden" name="_method" value="delete">
    <input type="submit" name="delete" value="Delete Section">
  </form>

</form>

With the second <form> section both submit buttons point to delete /section/:id. Without the second <form> both submit buttons point to put /section/:id.

I would like to have a single form with a button for Updateing and Deleting, but it seems that the any submit button has the value of whatever the last html verb used. Is this even possible to do with sinatra? or am I stuck using <a> links instead of buttons?

Don Graziano
  • 431
  • 5
  • 15
  • 2
    Sinatra cannot work magic, HTML standards don't allow nested forms: http://stackoverflow.com/questions/379610/can-you-nest-html-forms. Duplicate then? – tokland Nov 03 '11 at 19:30
  • So, Thats why my form code doesn't work. But I already know that it doesn't work, now I know why. But I still don't know if there is a way to have a form with multiple submit buttons/actions. – Don Graziano Nov 03 '11 at 19:37
  • Yes, you can have multiple submit buttons. The name of the input should go into the params as key, isn't this the case? – tokland Nov 03 '11 at 19:39
  • haha I don't know! thats why I'm asking... So... it sounds like what I am looking for is maybe a conditional route? ie. if 'update' is clicked then do `put /section/:id` else if 'delete' is clicked then do `delete /section/:id`. But Sinatra needs the HTML verb passed in as that hidden field, I don't see how just getting the prams of the value of the particular submit button would give me that condition. – Don Graziano Nov 03 '11 at 19:45
  • 1
    Perhaps have two routes like `post "/section/update/:id" do...` and `post "/section/delete/:id" do...` ? Puts and delete aren't properly supported across browsers anyway. You still can't nest the forms though, you'll need to either use javascript or redesign the form, or perhaps have a conditional inside a route based on the submit button pressed (the value is passed as the key as @tokland mentions). – iain Nov 09 '11 at 04:57
  • possible duplicate of [Multiple submit buttons in an HTML form](http://stackoverflow.com/questions/48/multiple-submit-buttons-in-an-html-form) – wich Oct 17 '12 at 09:36

1 Answers1

0

Now I know this now not valid syntax, javascript is required for this kind of magic.

Don Graziano
  • 431
  • 5
  • 15