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.

152 CHAPTER 7 Basic access control<br />

<br />

<br />

<br />

At the moment, admin_root_path doesn’t exist. To define it, open config/routes.rb<br />

and change the namespace definition from this<br />

namespace :admin do<br />

resources :users<br />

end<br />

to this:<br />

namespace :admin do<br />

root :to => "base#index"<br />

resources :users<br />

end<br />

When you rerun the feature, it fails because you don’t have an index action for the<br />

Admin::BaseController controller:<br />

When I follow "Admin"<br />

The action 'index' could not be found for Admin::BaseController<br />

Let’s add that now.<br />

7.5.2 The index action<br />

Open app/controllers/admin/base_controller.rb, and add the index action so the class<br />

definition looks like the following listing.<br />

class Admin::BaseController < ApplicationController<br />

before_filter :authorize_admin!<br />

def index<br />

end<br />

end<br />

Listing 7.20 app/controllers/admin/base_controller.rb<br />

You define the action here to show users that this controller has an index action. The<br />

next step is to create the view for the index action by creating a new file at app/views/<br />

admin/base/index.html.erb and filling it with the following content:<br />

<br />

Welcome to Ticketee's Admin Lounge. Please enjoy your stay.<br />

You needn’t wrap the link in an admins_only here because you’re inside a page that’s<br />

visible only to admins. When you run the feature, you don’t get a message saying The<br />

action 'index' could not be found even though you should. Instead, you get this:<br />

When I follow "New User"<br />

no link with title, id or text 'New User' found

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

Saved successfully!

Ooh no, something went wrong!