5

In my first Node project Express/Express-resource libraries are used and Jade for templating.

According to docs default mappings are generated. Among them we can find:

PUT     /forums/:forum       ->  update

Yet, I do not see easy way to submit values.

How to submit creation/update?

Jade form can be created easily and body parser as well, but how to submit this form? Please note that express-resource defines PUT method (not POST).

nrph
  • 335
  • 7
  • 18

1 Answers1

6

From the Express guide:

When using methods such as PUT with a form, we can utilize a hidden input named _method, which can be used to alter the HTTP method. To do so we first need the methodOverride middleware, which should be placed below bodyParser so that it can utilize it’s req.body containing the form values.

So:

app.use(express.bodyParser());
app.use(express.methodOverride());

And in your form:

<input type="hidden" name="_method" value="put">

Update: As I understand the new comments from the asker, nrph wants a way to submit a form with the PUT method, using ajax. Here is a solution using jQuery:

// Use this submit handler for all forms in document
$(document).on('submit', 'form', function(e) {
  // Form being submitted
  var form = e.currentTarget;
  // Issue an ajax request
  $.ajax({
    url: form.action,          // the forms 'action' attribute
    type: 'PUT',               // use 'PUT' (not supported in all browsers)
                               // Alt. the 'method' attribute (form.method)
    data: $(form).serialize(), // Serialize the form's fields and values
    success: function() {},
    error: function() {}
  });
  // Prevent the browser from submitting the form
  e.preventDefault();
});
Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
  • Aren't these put mappings created to be handled in ajax way? – nrph Mar 16 '12 at 14:45
  • Excuse me? I don't understand the question. – Linus Thiel Mar 16 '12 at 14:47
  • According to this http://stackoverflow.com/a/7257509/450441 comment most modern browsers support PUT with XMLHttpRequest. So it gives an idea that maybe ajax should be the most obvious option to submit values with express. – nrph Mar 16 '12 at 14:52
  • Oh sure, if your browser can issue a `PUT` this is a non-issue. – Linus Thiel Mar 16 '12 at 15:00
  • So you are suggesting just to use POST? – nrph Mar 16 '12 at 15:07
  • I'm suggesting you use `POST` (which is cross-browser compatible) with a hidden `_method` parameter set to `put`, which together with the `methodOverride` middleware will resolve to your `PUT` handler. – Linus Thiel Mar 16 '12 at 15:17
  • Alright, thanks for your insights. I seemed strange that creators of express-resource didn't create the most obvious POST mappings. As a express rookie I'm still looking for reason behind that. – nrph Mar 16 '12 at 15:26
  • You're still confused. Express provides that through `methodOverride`. Can't really do much about browsers only having support for `GET` and `POST`. – Linus Thiel Mar 16 '12 at 15:29
  • 1
    That preventDefault() needs to be the first statement or the form will still submit. – rkulla Jan 16 '13 at 01:36