27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

158 CHAPTER 7 Basic access control<br />

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

And I press "Update User"<br />

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

And I should see "Email is invalid"<br />

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

you discover the show action is missing:<br />

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

The action 'show' could not be found for Admin::UsersController<br />

7.7.1 The show action<br />

Define the show action in the Admin::UsersController, shown in listing 7.28, directly<br />

under the index action, because grouping the different parts of CRUD is conventional.<br />

The method you define is blank because you need to use a before_filter to<br />

find the user, as you’ve done in other controllers to find other resources.<br />

def show<br />

end<br />

Listing 7.27 app/controllers/admin/users_controller.rb<br />

You call the method to find the user object find_user and define it under the actions<br />

in this controller, like this:<br />

private<br />

def find_user<br />

@user = User.find(params[:id])<br />

end<br />

You then need to call this method using a before_filter, which should run before<br />

the show, edit, update, and destroy actions. Put this line at the top of your class definition<br />

for Admin::UsersController:<br />

before_filter :find_user, :only => [:show, :edit, :update, :destroy]<br />

With this method in place, you can write the template for the show action to make this<br />

step pass. This file goes at app/views/admin/users/show.html.erb and uses the following<br />

code:<br />

<br />

<br />

Now when you run bin/cucumber features/editing_users.feature, the step that<br />

previously failed passes, and you’re on to the next step:<br />

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

And I follow "Edit User"<br />

The action 'edit' could not be found for Admin::UsersController<br />

Good, you’re progressing nicely. You created the show action for the Admin::Users-<br />

Controller, which displays information for a user to a signed-in admin user. Now you<br />

need to create the edit action so admin users can edit a user’s details.

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

Saved successfully!

Ooh no, something went wrong!