开发者

Nested resources in railway.js: Says cannot POST

开发者 https://www.devze.com 2023-03-28 10:33 出处:网络
I am trying out an MVC framework called railway.js (which sits on top of Node, Express, Mongoose, and Mongo).

I am trying out an MVC framework called railway.js (which sits on top of Node, Express, Mongoose, and Mongo).

I'm trying to get nested res开发者_运维问答ources to work. I did the following scaffolding commands:

railway g scaffold user name email description 
railway g scaffold setup title description

Then I changed the routes.js file to:

exports.routes = function (map) {
  map.resources('users',function(user) {
    user.resources('setups');
  });
});

Doing railway r gives what I hoped for:

     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

When I start up the server, add a user (happens to be 4e4b61e39f0d60d834000002), then go to http://localhost:3000/users/4e4b61e39f0d60d834000002/setups/new, it says I "cannot POST".

What am I missing? What's a good debugging approach?

I also tried adding an element into the UserSchema object: setups: [SetupSchema]. (Shouldn't we have to do this?)

Thanks in advance.


in your 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));
}

in your setup/form.jade:

- form_for( setup, { method: 'POST', action: path_to.user_setups(user) }, function(form){
    //layout your form
- });


Look at file app/views/setups/new.ejs:

<h1>New setup</h1>

<% form_for(setup, {action: path_to.setups, method: 'POST', id: "setup_form"}, function (form) { %>
  <%- partial('setups/form.ejs', {locals: {form: form, setup: setup}}) %>
  <%- form.submit('Create setup') %> or
  <%- link_to('Cancel', path_to.setups) %>
<% });%>

It refers to not existing route path_to.setups, you have to change it to correct route path_to.user_setups:

<h1>New setup</h1>

<% form_for(setup, {action: path_to.user_setups(user), method: 'POST', id: "setup_form"}, function (form) { %>
  <%- partial('setups/form.ejs', {locals: {form: form, setup: setup}}) %>
  <%- form.submit('Create setup') %> or
  <%- link_to('Cancel', path_to.setups) %>
<% });%>

So, you will POST to /users/.../setups instead of POST to /users/.../setups/new.

Please note that you need pass user as first argument of path_to helper, so railway will build correct route for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消