RESTful问题/答案设计?

时间:2010-03-30 11:27:43

标签: ruby-on-rails api rest url-design

这是我正在进行的一个玩具项目。

我的应用包含多项选择答案的问题。

问题网址采用以下格式,GET& POST映射到问题控制器上的不同操作。

GET:   url.com/questions/:category/:difficulty      => 'ask'
POST:  url.com/questions/:category/:difficulty      => 'answer'

我想知道是否值得将其重新设计为RESTful风格。我知道我需要将答案作为一种资源来介绍,但我很难想到一个能够自然地回答这个问题的网址。

重新设计是否值得?你会如何构建网址?

2 个答案:

答案 0 :(得分:3)

我认为如果你映射会更好:

POST url.com/:category/:difficulty/questions => ask question
GET  url.com/questions                       => list all questions
GET  url.com/:category/:difficulty/questions => list questions for category and difficulty
GET  url.com/questions/:id                   => show question
POST url.com/questions/:id                   => add answer
PUT  url.com/questions/:id                   => edit question
PUT  url.com/questions/:question_id/:id      => edit answer with id :id
GET  url.com/questions/:question_id/:id      => show question with highlighted answer (like it is here on SO)

如果你愿意:

POST:  url.com/questions/:category/:difficulty      => 'answer'

然后你只能有一个具有指定类别和难度的问题。

答案 1 :(得分:2)

您不一定需要将答案视为单独的资源:您可以将答案视为问题的一个要素。事实上,我认为这样会更好 - 除非与问题挂钩,否则答案并不值得。

相关问题