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.

202 CHAPTER 8 More authorization<br />

Listing 8.16 app/views/admin/permissions/index.html.erb<br />

Permissions for <br />

:put do %><br />

<br />

<br />

Project<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

This template defines the table discussed earlier. It provides a header row of permission<br />

titles and rows for each project containing that project’s name followed by check<br />

boxes for each of the permissions. In this view, you use the form_tag, which generates<br />

a form that points to the path specified by its first argument. You also use a<br />

permissions helper method B, which isn’t currently defined but will provide a list of<br />

permissions to iterate through to check on each project.<br />

Right underneath where you use permissions, you use another helper method<br />

called cycle B. This method is built into Rails and cycles through its given arguments<br />

for each iteration of whatever it’s encapsulated in, so when this page displays the first<br />

project, the method sets the class of that tr tag to odd and the second one to even. It<br />

cycles between these two classes until it runs out of projects. This is how you can easily<br />

get different rows in this table to be styled differently.<br />

Before we look at how to define the permissions method, run bin/cucumber<br />

cucumber features/assigning_permissions.feature:<br />

undefined local variable or method<br />

'update_user_permissions_path' [...]<br />

You get an error because you haven’t yet defined the route for the form. The<br />

Admin::PermissionsController serves a different purpose than the standard REST<br />

controllers. For this controller, you use the update action to update a whole slew of<br />

permissions rather than a single one. To map to this action by using the update<br />

B<br />

C

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

Saved successfully!

Ooh no, something went wrong!