0

我正在上 saas 课程,在做作业 2 时,rails 应用程序会生成参数化 URL,例如http://localhost:3000/movies?sort=title

但是页面上的其他 URL 类似于http://localhost:3000/movies/newhttp://localhost:3000/movies/1。我想知道为什么排序没有被解析为像 /movies/sort/title 这样的宁静 URL。

我们什么时候创建restful URL,什么时候使用参数化URL?

4

1 回答 1

1

REST(由 Rails 使用)对资源进行操作。具体来说,它使用 HTTP 动词(GET、POST、PUT、DELETE)对资源进行操作。

假设你有一个电影模型。您可能有一个定义以下路线的电影资源:

GET '/movies' - Gets a list of movies
GET '/movies/new' - Gets the form to create a new movie
POST '/movies' - Creates a new movie
GET '/movies/:id' - Gets the details about the movie with :id
GET '/movies/:id/edit' - Edits the movie with :id
DELETE '/movies/:id' - Deletes the movie with :id
PUT '/movies/:id' - Updates the movie with :id

另一方面,排序是向 Rails 提供有关请求的附加信息的一种方式。因此,如果您要对模型或资源执行 CRUD 操作,您应该使用 RESTful 路由(如 railsguide 所述),否则您可能需要一个参数,或者您可以考虑使用javascript!

请注意,没有什么可以阻止您实现路由,就像'/movies/sort/title'它不是 RESTful 路由并且需要routes.rb文件中的自定义路由一样。只需阅读我上面链接的 railsguide 即可了解完整的故事。

于 2012-03-12T13:08:40.183 回答