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.

27 scenarios (27 passed)<br />

221 steps (221 passed)<br />

# and<br />

14 examples, 0 failures, 7 pending<br />

Great! Let’s push that:<br />

Creating admin users<br />

git add .<br />

git commit -m "Added the ability to create users through the admin backend"<br />

git push<br />

Although this functionality allows you to create new users through the admin backend,<br />

it doesn’t let you create admin users. That’s up next.<br />

7.6 Creating admin users<br />

To create admin users, you need a check box on the form that, when clicked, sets the<br />

user’s admin field to true. But, because the admin attribute isn’t on the list of accessible<br />

attributes (attr_accessible inside app/models/user.rb), it can’t be mass-assigned<br />

as the other fields can. Therefore, you must manually set this parameter in the controller<br />

before the user is saved.<br />

To get started, let’s add another scenario to the features/creating_users.feature<br />

using the code from the following listing.<br />

Listing 7.24 features/creating_users.feature<br />

Scenario: Creating an admin user<br />

When I fill in "Email" with "newadmin@ticketee.com"<br />

And I fill in "Password" with "password"<br />

And I check "Is an admin?"<br />

And I press "Create User"<br />

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

And I should see "newadmin@ticketee.com (Admin)"<br />

Now when you run bin/cucumber features/creating_users.feature, it fails on the<br />

"Is an admin?" step:<br />

cannot check field, no checkbox with id, name,<br />

or label 'Is an admin?' found (Capybara::ElementNotFound)<br />

You want to add this check box to the form for creating users, which you can do by<br />

adding the following code to the form_for block inside app/views/admin/users/<br />

_form.html.erb:<br />

<br />

<br />

<br />

<br />

With this check box in place, when you run bin/cucumber features/creating<br />

_users.feature, you’re told "newadmin@ticketee.com (Admin)" can’t be found anywhere<br />

on the page:<br />

155

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

Saved successfully!

Ooh no, something went wrong!