Hartl's Rails Tutorial - Solutions for Ch 10 Exercises
26 May 2014
My solutions for Michael Hartl’s Rails tutorial, Ch. 10 exercises. Some of these exercises are quite challenging; StackOverflow is a great resource if you get stuck.
####Add tests for the sidebar micropost counts (including proper pluralization).
For my implementation, I tested for the singular count, when there is only one micropost.
In spec/requests/static_pages_spec.rb, add test inside “for signed-in users” section. In the “micropost counts” test, remove one of the two created test microposts, and should expect page to have only one micropost.
##2.
####Add tests for micropost pagination.
Like exercise 1, I probably could’ve included pagination test inside “for signed-in users” section, but I created a separate section to work with a new set of microposts.
In spec/requests/static_pages_spec.rb, test should verify that page has a pagination div when there are more than 30 microposts:
##3.
####Refactor the Home page to use separate partials for the two branches of the if-else statement.
The app/views/static_pages/home.html.erb file is cluttered, so create two new partials: app/views/shared/_user_homepage.html.erb and app/views/shared/_homepage.html.erb.
The first partial displays a user-based homepage, while the later displays generic homepage for users that are not signed in.
And now the refactored app/views/static_pages/home.html.erb:
After completing this exercise, I think it may be more appropriate to put these partials in app/views/static_pages as opposed to app/views/shared directory. Nevertheless, the partials still work as intended.
##4.
####Write a test to make sure delete links do not appear for microposts not created by the current user.
In spec/requests/authentication_pages_spec.rb, add the following test:
##5.
####Using partials, eliminate the duplication in the delete links from Listing 10.43 and Listing 10.44.
First create a new partial, app/views/shared/_delete_link.html.erb:
And update app/views/microposts/_micropost.html.erb:
Note that we passed in different objects (feed_item and micropost) for each view.
##6.
####Very long words currently break our layout, as shown in Figure 10.18. Fix this problem using the wrap helper defined in Listing 10.47. Note the use of the raw method to prevent Rails from escaping the resulting HTML, together with the sanitize method needed to prevent cross-site scripting. This code also uses the strange-looking but useful ternary operator (Box 10.1).
This exercise provides us with the code to handle long words. We just need to implement in our application.
If not already created, create app/helpers/microposts_helper.rb and add helper methods:
Next, update views with wrap method. In app/views/microposts/_micropost.html.erb:
And in app/views/shared/_feed_item.html.erb:
##7.
####(challenging) Add a JavaScript display to the Home page to count down from 140 characters.
My javascript and jQuery skills are still rudimentary, so I used this solution from StackOverFlow.
Update: When I first attempted this problem, I could not understand much, so don’t get discouraged. After starting the Javascript course on Odin, I was able to start refactoring the code from StackOverFlow.