我正在尝试一个名为railway.js的MVC框架(位于Node,Express,Mongoose和Mongo之上)。
我正在尝试使嵌套资源起作用。我做了以下脚手架命令:
railway g scaffold user name email description
railway g scaffold setup title description
然后我将route.js文件更改为:
exports.routes = function (map) {
map.resources('users',function(user) {
user.resources('setups');
});
});
做
railway r
给出了我希望的: user_setups GET /users/:user_id/setups.:format? setups#index
user_setups POST /users/:user_id/setups.:format? setups#create
new_user_setup GET /users/:user_id/setups/new.:format? setups#new
edit_user_setup GET /users/:user_id/setups/:id/edit.:format? setups#edit
user_setup DELETE /users/:user_id/setups/:id.:format? setups#destroy
user_setup PUT /users/:user_id/setups/:id.:format? setups#update
user_setup GET /users/:user_id/setups/:id.:format? setups#show
users GET /users.:format? users#index
users POST /users.:format? users#create
new_user GET /users/new.:format? users#new
edit_user GET /users/:id/edit.:format? users#edit
user DELETE /users/:id.:format? users#destroy
user PUT /users/:id.:format? users#update
user GET /users/:id.:format? users#show
当我启动服务器时,添加一个用户(可能是4e4b61e39f0d60d834000002),然后转到http://localhost:3000/users/4e4b61e39f0d60d834000002/setups/new,它说我“无法发布”。
我想念什么?有什么好的调试方法?
我还尝试将一个元素添加到UserSchema对象中:
setups: [SetupSchema]
。 (我们不必这样做吗?)提前致谢。
最佳答案
在您的setup_controller中:
before(loaduser);
...
function loadUser () {
User.findById(req.params.user_id, function (err, user) {
if (err || !user) {
redirect(path_to.users);
} else {
// this is where we make the actual user object accessible inside the view templating
this.user = user;
next();
}
}.bind(this));
}
在您的setup / form.jade中:
- form_for( setup, { method: 'POST', action: path_to.user_setups(user) }, function(form){
//layout your form
- });