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.

Namespace-based CRUD<br />

git add .<br />

git commit -m "Added admin namespaced users controller"<br />

git push<br />

7.5 Namespace-based CRUD<br />

Now that only admins can access this namespace, you can create the CRUD actions for<br />

this controller too, as you did for the TicketsController and ProjectsController<br />

controllers. Along the way, you’ll also set up a homepage for the admin namespace.<br />

For this new CRUD resource, you first write a feature for creating a user and put it<br />

at features/creating_users.feature, as shown in the following listing.<br />

Listing 7.19 features/creating_users.feature<br />

Feature: Creating Users<br />

In order to add new users to the system<br />

As an admin<br />

I want to be able to add them through the backend<br />

Background:<br />

Given there are the following users:<br />

| email | password | admin |<br />

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

And I am signed in as them<br />

Given I am on the homepage<br />

When I follow "Admin"<br />

And I follow "Users"<br />

When I follow "New User"<br />

Scenario: Creating a new user<br />

And I fill in "Email" with "newbie@ticketee.com"<br />

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

And I press "Create User"<br />

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

Scenario: Leaving email blank results in an error<br />

When I fill in "Email" with ""<br />

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

And I press "Create User"<br />

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

And I should see "Email can't be blank"<br />

When you run this feature using bin/cucumber features/creating_users.feature,<br />

the first four steps pass; but when you follow the Admin link, it fails because the link<br />

doesn’t exist yet:<br />

When I follow "Admin"<br />

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

7.5.1 Adding a namespace root<br />

Of course, you need this link for the feature to pass, but you want to show it only for<br />

admins. You can use the admins_only helper you defined earlier and put the link in<br />

app/views/layouts/application.html.erb in the nav element:<br />

151

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

Saved successfully!

Ooh no, something went wrong!