Hartl's Rails Tutorial - Solutions for Ch 9 Exercises
21 May 2014My solutions for Michael Hartl’s Rails tutorial, Ch. 9 exercises. I found a lot of help on StackOverflow, which I recommend if you’re looking for detailed-explanations, discussion, or different ways to complete some of these exercises.
##Exercises
####By issuing a PATCH request directly to the update method, verify that the admin attribute isn’t editable through the web. Be sure to get first to Red, and then to Green. (Hint: Your first step should be to add admin to the list of permitted parameters in user_params
.)
In app/controllers/users_controller.rb
, add :admin
to permitted parameters:
Write a test in spec/requests/user_pages_spec.rb
:
Run rspec spec/requests/user_pages_spec.rb
, and result should be failing test:
Remove :admin
from user_params
, and re-run the test. Now, the result should be a passing test.
####Arrange for the Gravatar “change” link in Listing 9.3 to open in a new window (or tab). Hint: Search the web; you should find one particularly robust method involving something called _blank
.
In app/views/users/edit.html.erb
, modify the <a>
tag to include target
attribute:
####The current authentication tests check that navigation links such as “Profile” and “Settings” appear when a user is signed in. Add tests to make sure that these links don’t appear when a user isn’t signed in.
In spec/requests/authentication_pages_spec.rb
, add the following tests:
Ideally, you want the test to go from RED (fail) to GREEN (pass), so we can insert a “Profile” link in the header (app/views/layouts/_header.html.erb
) where the user is not signed in:
Run rspec spec/requests/authentication_pages_spec.rb
and confirm failing test:
Now, remove the added “Profile” link from header and confirm passing test.
####Use the sign_in
test helper in as many places as you can find.
In your spec files, replace:
with:
####Remove the duplicated form code by refactoring the new.html.erb
and edit.html.erb
views to use the partial in Listing 9.49. Note that you will have to pass the form variable f explicitly as a local variable, as shown in Listing 9.50. You will also have to update the tests, as the forms aren’t currently exactly the same; identify the slight difference and update the tests accordingly.
Create app/views/users/_fields.html.erb
:
Refactor forms:
The :password_confirmation
label originally had different labels in each of the forms. Now that both forms are using the partial, need to make small update to test.
In spec/requests/user_pages_spec.rb
update label for password:confirmation
:
Run rspec test and confirm passing test.
####Signed-in users have no reason to access the new and create actions in the Users controller. Arrange for such users to be redirected to the root URL if they do try to hit those pages.
In app/controllers/users_controller.rb
, add a before_action
filter and helper method to prevent signed-in users from accessing the #new and #create actions.
In spec/requests/authentication_pages_spec.rb
, add tests to make sure signed-in user cannot acces #new and #create actions:
####Learn about the request
object by inserting some of the methods listed in the Rails API9 into the site layout. (Refer to Listing 7.1 if you get stuck.)
Pending…
####Write a test to make sure that the friendly forwarding only forwards to the given URL the first time. On subsequent signin attempts, the forwarding URL should revert to the default (i.e., the profile page). See Listing 9.51 for a hint (and, by a hint, I mean the solution).
As stated in the question, you can find solution in the tutorial.
####Modify the destroy
action to prevent admin users from destroying themselves. (Write a test first.)
This exercise is an additional layer of protection. In the tutorial, we prevent admin users from destroying themselves at the View layer. For this exercise, we prevent same thing at the Controller layer.
In spec/requests/authentication_pages_spec.rb
, write following test:
Run test and verify failing test.
In app/controllers/users_controller.rb
, modify the #destroy
action:
Run test and verify passing test.