I’m taking saas class and while doing the homework 2, the rails app generates parameterized URLs like http://localhost:3000/movies?sort=title.
However other URLs on the page are like http://localhost:3000/movies/new or http://localhost:3000/movies/1. I’m wondering why the sorting is not resolved as a restful URL like /movies/sort/title.
And when do we create restful URLs and when to use parameterized URLs?
REST (as used by Rails) operates on resources. Specifically it uses the HTTP verbs (GET, POST, PUT, DELETE) to operate on resources.
Assume you had a movie model. You might have a movies resource which would define the following routes:
Sorting on the other hand is a way of providing rails with additional information regarding the request. So, if you’re going to be doing the CRUD actions on a model or a resource you should be using RESTful routing (as described by the railsguide) but otherwise you may want a parameter, or you could consider sorting your data client side using javascript!
Note that there isn’t anything stopping you from implementing a route like
'/movies/sort/title'it just isn’t a RESTful route and requires custom routes in yourroutes.rbfile. Just read the railsguide I linked above for the complete story.