【问题标题】:CompoundJs, ExpressJs routesCompoundJs、ExpressJs 路由
【发布时间】:2013-12-03 13:46:48
【问题描述】:

我有 NodeJs:0.10.22 和 CompoundJs:1.1.7-11

我有以下控制器代码:

module.exports = Rose;

function Rose(init){

}

Rose.prototype.index = function(c){
    c.send('Controller ROSE,  Function Index');
};

Rose.prototype.thorne = function thorne(c){
    c.send('Controller ROSE, Function Thorne');
};

我在 routes.js 文件中定义了以下路由:

exports.routes = function (map) {

    map.resources('rose', function(flower){
         //flower.get('thorne', '#thorne');
        flower.get('thorne');
    });
};

我已经在 routes.js 中尝试了 map.resources 中的两行(目前已标记,但之前已使用)。

以下网址有效:

http://localhost:3000/rose

但以下网址不起作用:

http://localhost:3000/rose/thorne

它显示以下错误:

Express
500 Error: Undefined action rose#show(/rose/thorne)

有人可以指导我做错了什么以及如何纠正它。

【问题讨论】:

    标签: node.js express compoundjs


    【解决方案1】:

    CompoundJS CRUD 允许查看模型列表以及操作单个数据的方法。使用.resources 时,会生成多个路由。您可以通过运行compound routes(或简称compound r)来查看它们。下面是一些使用 roses(复数)的示例:

    roses GET        /roses.:format?          roses#index - Should return a list of roses
    rose GET         /roses/:id.:format?      roses#show  - Displays a single rose
    edit_rose GET    /roses/:id/edit.:format? roses#edit  - Brings up an edit form for a single rose
    

    在为一朵花映射路线时,compoundjs 期望您将一朵花映射到一朵玫瑰。路由定义为:

    thorne_rose GET  /roses/:rose_id/thorne   roses#thorne
    

    您必须传入玫瑰 ID 才能访问该特定玫瑰的刺。但是,如果您打算只拥有一朵玫瑰(没有玫瑰列表或其他任何东西),您可以将单例选项添加到资源中:

    map.resources('rose', { singleton: true }, function(flower) {
        flower.get('thorne');
    });
    

    这也将消除索引路由,改为使用Rose.prototype.show。在典型的 CRUD 中,索引列出了模型中的所有值。由于只有一个值,所以不需要列表页。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      相关资源
      最近更新 更多