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.

200 CHAPTER 8 More authorization<br />

The path for this link_to (which is not yet defined) takes you to the Admin<br />

::PermissionsController’s index action. To get this link_to to work, define that<br />

permissions are nested under users in the config/routes.rb, and add the admin<br />

namespace in the definition using this code:<br />

namespace :admin do<br />

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

resources :users do<br />

resources :permissions<br />

end<br />

end<br />

With these changes in the config/routes.rb file, the admin_user_permissions_path<br />

used in the link_to will now be defined. When you run the feature using bin/cucumber<br />

features/assigning_permissions.feature, you see there’s more to be done for this<br />

step:<br />

And I follow "Permissions"<br />

uninitialized constant Admin::PermissionsController<br />

➥(ActionController::RoutingError)<br />

Ah, of course! You must create the controller for this link!<br />

THE PERMISSIONS CONTROLLER<br />

You can generate the Admin::PermissionsController file by running this command:<br />

rails g controller admin/permissions<br />

Along with an app/controllers/admin/permissions_controller.rb file, this command<br />

generates other goodies, such as a helper and a directory for the views at app/views/<br />

admin/permissions. Before you go further, you must modify this file to make the class<br />

inherit from the right place so that only admins can access it. Open the file, and<br />

change the first line to this:<br />

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

This line makes the controller inherit from the Admin::BaseController class, which<br />

restricts all actions in this controller to only admin users. When you run the feature<br />

again, the index action is missing from this controller:<br />

And I follow "Permissions"<br />

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

Obviously, you need to define this action before you carry on. Inside this action, load<br />

all the permissions for the user you’re currently looking at, and then, with the view,<br />

display a page from which an admin can choose what permissions this user has on<br />

each project. It’d be helpful if this user was loaded by a before_filter because you’ll<br />

need it for the action that updates the permissions later. With all this in mind, update<br />

the entire controller to resemble the following listing.

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

Saved successfully!

Ooh no, something went wrong!