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.

Namespace-based CRUD<br />

This unexpected output occurs because the Admin::UsersController inherits from<br />

Admin::BaseController, where you just defined an index method. By inheriting<br />

from this controller, Admin::UsersController also inherits its views. When you<br />

inherit from a class like this, you get the methods defined in that class too. You can<br />

override the index action from Admin::BaseController by redefining it in<br />

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

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

class Admin::UsersController < Admin::BaseController<br />

def index<br />

@users = User.all(:order => "email")<br />

end<br />

end<br />

Next, you rewrite the template for this action, which lives at app/views/admin/users/<br />

index.html.erb, so it contains the New User link and lists all the users gathered up by<br />

the controller, as shown in the following listing.<br />

Listing 7.22 app/views/admin/users/index.html.erb<br />

<br />

<br />

<br />

<br />

<br />

<br />

In this example, when you specify a Symbol as an element in the route for the<br />

link_to, Rails uses that element as a literal part of the route generation, making it use<br />

admin_user_path rather than user_path. You saw this in chapter 5 when you used it<br />

with [:edit, @project, ticket], but it bears repeating here.<br />

When you run bin/cucumber features/creating_users.feature again, you’re<br />

told the new action is missing:<br />

When I follow "New User"<br />

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

7.5.3 The new action<br />

Let’s add the new action Admin::UsersController now by using this code:<br />

def new<br />

@user = User.new<br />

end<br />

And let’s create the view for this action at app/views/admin/users/new.html.erb:<br />

New User<br />

<br />

153

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

Saved successfully!

Ooh no, something went wrong!