Hartl's Rails Tutorial - Solutions for Ch 11 Exercises
29 May 2014
My solutions for Michael Hartl’s Rails tutorial, Ch. 11 exercises. Some of my solutions didn’t seem as robust this time around, but I posted for others to compare.
Ch. 11 also has several add-on features (messaging, password reminders, REST API, etc.), which I plan to work on in the near future.
####Add tests for destroying relationships associated with a given user (i.e., as implemented by dependent: :destroy in Listing 11.4 and Listing 11.16). Hint: Follow the example in Listing 10.15.
I added one test to check that an associated relationship is destoryed when user is destoryed.
In spec/models/user_spec.rb, you can add test inside describe “following” block:
Verify failing test when you remove dependent: :destroy from the has_many association in User model:
Once you get failing test, add back dependent: :destroy and verify passing test.
##2.
####The respond_to method seen in Listing 11.38 can actually be hoisted out of the actions into the Relationships controller itself, and the respond_to blocks can be replaced with a Rails method called respond_with. Prove that the resulting code, shown in Listing 11.47, is correct by verifying that the test suite still passes. (For details on this method, do a Google search on “rails respond_with”.)`
Not too much to do here, but simply replace code that Hartl provides. Replace code, re-run rSpec tests, and verify passing tests.
##3.
####Refactor Listing 11.31 by adding partials for the code common to the following/followers pages, the Home page, and the user show page.
I coudldn’t find much to refactor in this case.
Here’s Listing 11.31:
I updated the user profile with the shared/user_info partial and left the rest as is:
##4.
####Following the model in Listing 11.19, write tests for the stats on the profile page.
With the help of Listing 11.19, this additional test seemed pretty straight forward.
In spec/reqeuests/user_pages.rb, inside the describe “profile page” block, add following tests:
In app/views/users/show.html.erb, verify failing tests by commenting out <%= render 'shared/stats %>: