More Related Content
Similar to Sails workshop1 (20)
Sails workshop1
- 22. 環境設定
npm install -g sails
cd 好きなディレクトリへ
sails new SailsWorkShop
cd SailsWorkShop
npm install その後エディタ起動
- 25. User.js (model)
schema: true,
attributes: {
name: {
type: 'string',
required: true
},
title: {
type: 'string'
},
email: {
type: 'string',
email: true,
required: true,
unique: true
},
encryptedPassword: {
type: 'string'
}
}
- 26. module.exports = function(req, res, next){
res.locals.flash = {};
if(!req.session.flash) return next();
res.locals.flash =
_.clone(req.session.flash);
return next();
};
policies/flash.js作成
- 28. user/index.ejs
<div class="container">
<h3>Users</h3>
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Title</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% _.each(users, function(user) {%>
<tr data-id="<%= user.id %>" data-
model="user">
<td><%= user.id %></td>
<td><%= user.name %></td>
<td><%= user.title %></td>
<td><%= user.email %></td>
<td><a href='/user/show/<%= user.id %>'
class="btn btn-sm btn-primary">Show</a></
td>
<td><a href='/user/edit/<%= user.id %>'
class="btn btn-sm btn-warning">Edit</a></td>
<td><form action="/user/destroy/<%=
user.id %>" method="POST">
<input type="hidden"
name="_method" value="delete"/>
<input type="submit" class="btn btn-
sm btn-danger" value="Delete"/>
<input type="hidden" name="_csrf"
value="<%= _csrf %>"/>
</form></td>
<!-- <td><a href='/user/destroy/<%=
user.id %>' class="btn btn-sm btn-
danger">Delete</a></td> -->
</tr>
<% })%>
</table>
</div>
- 29. <form action="/user/create" method="POST" id="sign-up-form" class="form-
signin">
<h2 class="form-signin-heading">Create an account</h2>
<input type="text" class="form-control" placeholder="your name"
name="name"/>
<input type="text" class="form-control" placeholder="your title" name="title"/
>
<input type="text" class="form-control" placeholder="email" name="email"/>
<input type="password" id="password" class="form-control"
placeholder="password" name="password"/>
<input type="password" class="form-control" placeholder="password
confirmation" name="confirmation"/>
<br/>
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Create
Account"/>
<input type="hidden" name="_csrf" value="<%= _csrf %>"/>
</form>
user/new.ejs
- 30. <form action="/user/update/<%= user.id %>" method="POST"
class="form-signin">
<h2>編集画面</h2>
<input value="<%= user.name %>" name="name"
type="text" class="form-control"/>
<input value="<%= user.title %>" name="title" type="text"
class="form-control"/>
<input value="<%= user.email %>" name="email"
type="text" class="form-control"/>
<input value="編集" type="submit" class="btn btn-lg btn-
primary btn-block"/>
<input value="<%= _csrf %>" name="_csrf" type="hidden"/>
</form>
user/edit.ejs
- 31. <div class="container">
<h1><%= user.name %></h1>
<h3><%= user.title %></h3>
<hr>
<h3>Contact: <%= user.email %> </h3>
<a class="btn btn-medium btn-primary"
href="/user/edit/<%= user.id%>">Edit</a>
</div>
user/show.ejs
- 41. show
// 個別ページ id -> そのidのuserオブジェクトをviewへ送り描画
show: function(req, res, next){
User.findOne(req.param('id'), function foundUser(err, user){
if(err) return next(err);
if(!user) return next();
res.view({
user:user
});
});
},
- 45. update
update: function(req, res, next){
User.update(req.param('id'), req.params.all(),
function userUpdated(err){
if(err){
return res.redirect('/user/edit/'+
req.param('id'));
}
res.redirect('/user/show/' + req.param('id'));
})
},
- 48. destroy
destroy: function(req, res, next){
User.findOne(req.param('id'), function foundUser(err, user)
{
if(err)return next(err);
if(!user) return next("User doesn't exist.");
User.destroy(req.param('id'), function userDestroyed(err)
{
if(err) return next(err);
});
res.redirect('/user');
});
}
- 54. 【View】
・JSON返却
res.json({ hoge: hoge });
・view描画
res.view({ hoge: hoge });
【その他】
・リダイレクト
res.redirect(URL);
・よくあるエラー処理
if(err) return next(err)
if(!model) return next();
今回使ったSailsのまとめ