|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +## Part 3: Remember the sorting and filtering settings (70 points) |
| 5 | + |
| 6 | +OK, so the user can now click on the "Movie Title" or "Release Date" |
| 7 | +headings and see movies sorted by those columns, and can additionally |
| 8 | +use the checkboxes to restrict the listing to movies with certain |
| 9 | +ratings only. And we have preserved RESTfulness, because the URI itself |
| 10 | +always contains the parameters that will control sorting and filtering. |
| 11 | + |
| 12 | +The last step is to remember these settings. That is, if the user has |
| 13 | +selected any combination of column sorting and restrict-by-rating |
| 14 | +constraints, and then the user clicks to see the details of one of the |
| 15 | +movies (for example), when she clicks the Back to Movie List on the |
| 16 | +detail page, the movie listing should "remember" her sorting and |
| 17 | +filtering settings from before. |
| 18 | + |
| 19 | +(Clicking away from the list to see the details of a movie is only one |
| 20 | +example; the settings should be remembered regardless what actions the |
| 21 | +user takes, so that any time she visits the index page, the settings are |
| 22 | +correctly reinstated.) |
| 23 | + |
| 24 | +The best way to do the "remembering" will be to use the `session[]` |
| 25 | +hash. The session is like the `flash[]`, except that once you set |
| 26 | +something in the `session[]` it is remembered "forever" until you nuke the |
| 27 | +session with `session.clear` or selectively delete things from it with |
| 28 | +`session.delete(:some_key)`. That way, in the `index` method, you can |
| 29 | +selectively apply the settings from the `session[]` even if the incoming |
| 30 | +URI doesn’t have the appropriate `params[]` set. |
| 31 | + |
| 32 | +### Hints and caveats |
| 33 | + |
| 34 | +If the user explicitly includes new sorting/filtering settings in |
| 35 | +`params[]`, the session should not override them. Instead, these new |
| 36 | +settings should be remembered in the session. |
| 37 | + |
| 38 | +If a user unchecks all checkboxes, use the settings stored in the |
| 39 | +`session[]` hash, since it doesn't make sense for a user to uncheck all the |
| 40 | +boxes. |
| 41 | + |
| 42 | +To be RESTful, we want to preserve the property that a URI that results |
| 43 | +in a sorted/filtered view always contains the corresponding |
| 44 | +sorting/filtering parameters. Therefore, if you find that the incoming |
| 45 | +URI is lacking the right `params[]` and you're forced to fill them in from |
| 46 | +the `session[]`, the RESTful thing to do is to `redirect_to` the new URI |
| 47 | +containing the appropriate parameters. There is an important corner case |
| 48 | +to keep in mind here, though: if the previous action had placed a |
| 49 | +message in the `flash[]` to display after a redirect to the movies page, |
| 50 | +your additional redirect will delete that message and it will never |
| 51 | +appear, since the `flash[]` only survives across a single redirect. To fix |
| 52 | +this, use `flash.keep` right before your additional redirect. |
| 53 | + |
| 54 | +### When you're done with this part |
| 55 | + |
| 56 | +Deploy to Heroku by following the same process as before: |
| 57 | + |
| 58 | +```sh |
| 59 | +$ git commit -a -m "part 3 complete" |
| 60 | +$ git push heroku master |
| 61 | +``` |
0 commit comments