27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Deleting users<br />

Let’s make a commit for this new feature:<br />

git add .<br />

git commit -m "Added ability to edit and update users"<br />

git push<br />

With the updating done, there’s only one more part to go for your admin CRUD<br />

interface: deleting users.<br />

7.8 Deleting users<br />

There comes a time in an application’s life when you need to delete users. Maybe they<br />

asked for their account to be removed. Maybe they were being pesky. Or maybe you<br />

have another reason to delete them. Whatever the case, having the functionality to<br />

delete users is helpful.<br />

Keeping with the theme so far, you first write a feature for deleting users (using the<br />

following listing) and put it at features/deleting_users.feature.<br />

Listing 7.31 features/deleting_users.feature<br />

Feature: Deleting users<br />

In order to remove users<br />

As an admin<br />

I want to click a button and delete them<br />

Background:<br />

Given there are the following users:<br />

| email | password | admin |<br />

| admin@ticketee.com | password | true |<br />

| user@ticketee.com | password | false |<br />

And I am signed in as "admin@ticketee.com"<br />

Given I am on the homepage<br />

When I follow "Admin"<br />

And I follow "Users"<br />

Scenario: Deleting a user<br />

And I follow "user@ticketee.com"<br />

When I follow "Delete User"<br />

Then I should see "User has been deleted"<br />

When you run this feature, you get right up to the first step with no issue and then it<br />

complains:<br />

no link with title, id or text 'Delete' found (Capybara::ElementNotFound)<br />

Of course, you need the Delete link! Add it to the show template at app/views/admin/<br />

users/show.html.erb, right underneath the Edit User link:<br />

:delete,<br />

:confirm => "Are you sure you want to delete this user?" %><br />

You need to add the destroy action next, directly under the update action in<br />

Admin::UsersController, as shown in the following listing.<br />

161

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!