我正在参加saas课程,在做作业2时,rails app会生成参数化的网址,例如http://localhost:3000/movies?sort=title。
但是,页面上的其他网址如http://localhost:3000/movies/new或http://localhost:3000/movies/1。我想知道为什么排序没有被解决为像/ movies / sort / title这样的宁静URL。
我们什么时候创建了宁静的URL以及何时使用参数化的URL?
答案 0 :(得分: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路由(as described by the railsguide),否则您可能需要参数,或者您可以考虑使用对数据客户端进行排序的JavaScript!
请注意,没有什么能阻止您实现类似'/movies/sort/title'
的路由,它只是一个RESTful路由并且需要routes.rb
文件中的自定义路由。只需阅读我上面链接的railsguide就完整的故事。