1

I am new to MVC.

When I want to pass the form data from ejs file to controller, it does not work.

Create.ejs file

<!--create.ejs-->
<h2 class="col-sm-offset-1">Person Create form</h2>

<form action="/person/create" method="POST" class="form-horizontal">
<div class="form-group">
    <label class="control-label col-sm-2">Name: </label>
    <div class="col-sm-8">
        <input class="form-control" name="Person[name]" type="text">
    </div>
</div>

<div class="form-group">
    <label class="control-label col-sm-2">Age: </label>
    <div class="col-sm-8">
        <input class="form-control" name="Person[age]" type="number"
        min="0" max="120">
    </div>
</div>

<button class="btn btn-default col-sm-offset-2" type="submit" value="ADD">Submit</button>

PersonController.js

module.exports = {
create: function(req, res) {
if (req.method == "POST") {
    Person.create(req.body.Person).exec( function(err, model) {
        return res.send("Successfully Created!");
    });
} else {
    return res.view('person/create');
}
},

The result is that it cant get inside the (req.method == "POST") condition. Give me 404 error.

3
  • Where's your router? Commented Aug 2, 2018 at 9:46
  • I have but not showing in the question. Some things like '/person/create': { view: 'person/create' } Commented Aug 2, 2018 at 10:02
  • Verify if your route is of POST type Commented Aug 2, 2018 at 10:05

2 Answers 2

3

A 404 is a not found result.

When POSTing ensure you are targeting your Controller functions try:

'POST /person/create': 'person/PersonController.create',

Though why do you have a subfolder named person when you are using a controller?

Using Actions

In Sails v1.0 you would separate your controller functions into more manageable actions. A sub folder api/controllers/person makes more sense now.

Your routes.js file would read 'POST /person/create': {action: 'person/create'},

For simplicity I have removed req.body.Person as an array...

<form action="/person/create" method="POST" class="form-horizontal">

  <input type="hidden" name="_csrf" value="<%= _csrf %>" />

  <div class="form-group">
    <label class="control-label col-sm-2">Full Name: </label>
    <div class="col-sm-8">
      <input class="form-control" name="fullName" placeholder="John Johnson" type="text">
    </div>
  </div>

  <div class="form-group">
    <label class="control-label col-sm-2">Age: </label>
    <div class="col-sm-8">
      <input class="form-control" name="age" type="number" min="0" max="120">
    </div>
  </div>

  <button class="btn btn-default col-sm-offset-2" type="submit" value="ADD">Submit</button>

</form>

then your async function would be like...

module.exports = {


      friendlyName: 'Create Person.',
      description: 'Creating a new person.',


      exits: {

        success: {
          description: 'Person Created successfully.',
          viewTemplatePath: 'person/review',
        }

      },

      fn: async function (inputs, exits) {
        
        if (!this.req.me) throw {redirect: '/'};   

        let params = this.req.allParams();

        let person = await Person.create(params).fetch();
        
        return exits.success({person:person});

      }
}

Sign up to request clarification or add additional context in comments.

Comments

1

Like @Tejashwi suggested you need to change your route to a POST.

    'POST  /api/v1/create':   { action: 'create/person-create-form' },

1 Comment

You might want to include a solution that was not an action ie. 'POST /person/create': 'person/PersonController.create',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.