Reputation: 1
I'm trying to familiarize myself with Sails.js. I'm stuck with a problem : I cannot manage to call a controller when submitting a form.
Here is my form in my ejs file :
<form method="post" action="/calibers/add" enctype="multipart/form-data">
<label for="fname">Name of the new caliber:</label><br>
<input type="text" id="name" name="name" value="" placeholder="7,62x54 R">
<input type="submit" value="submit" class="btn">
</form>
Here is how the mapping is done in routes.js :
'GET /calibers/view': { action: 'calibers/view'},
'POST /calibers/add': { action: 'calibers/add'},
here is the called controller: add.js:
module.exports = {
friendlyName: 'Add calibers',
description: 'Adding new caliber to calibers list',
inputs: {
name: {
type: 'string',
required: true
},
},
exits: {
success: {
responseType: 'view',
viewTemplatePath: 'pages/welcome'
},
},
fn: async function (inputs, exits) {
console.log("Called function");
let newCal = await Caliber.create({name: inputs.name});
// All done.
if (!newCal) {
return exits.invalid({
message: 'New caliber could\'t be added'
});
}
return exits.success({
message: 'New caliber successfully added',
data: userRecord
});}
};
When I click on the submit button of my form, the controller never seems to be called, and I'm redirected to a simple "Forbidden". What is going on ? Thanks for you help.
Upvotes: 0
Views: 83
Reputation: 1078
Considering you're using the action2 format you should have:
The action Add in api/controllers/caliber/add
The routing:
'POST /api/v1/caliber/add': { action: 'caliber/add' }'
In the view file:
<form method="post" action="add" enctype="multipart/form-data">
Although I'd advise you use longer action names as you might have add in several controllers which might lead to issues.
It is all in the docs, please read them.
Upvotes: 1