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.

110 CHAPTER 5 Nested resources<br />

see that it can’t find the text for the first ticket because you’re not displaying any tickets<br />

on the show template yet:<br />

expected there to be content "Make it shiny!" in "[text]"<br />

5.2.1 Listing tickets<br />

To display a ticket on the show template, you can iterate through the project’s tickets<br />

by using the tickets method, made available by the has_many :tickets call in your<br />

model. Put this code at the bottom of app/views/projects/show.html.erb, as shown in<br />

the next listing.<br />

Listing 5.10 app/views/projects/show.html.erb<br />

<br />

<br />

<br />

# - <br />

<br />

<br />

<br />

TIP If you use a @ticket variable in place of the ticket variable in the<br />

link_to’s second argument, it will be nil. You haven’t initialized the<br />

@ticket variable at this point, and uninitialized instance variables are nil<br />

by default. If @ticket rather than the correct ticket is passed in here, the<br />

URL generated will be a projects URL, such as /projects/1, rather than the<br />

correct /projects/1/tickets/2.<br />

Here you iterate over the items in @project.tickets using the each method, which<br />

does the iterating for you, assigning each item to a ticket variable inside the block.<br />

The code inside this block runs for every single ticket. When you run bin/cucumber<br />

features/viewing_tickets.feature, you get this error:<br />

When I follow "Ticketee"<br />

no link with title, id or text 'Ticketee' found<br />

The reasoning behind wanting this not-yet-existing link is that when users click it, it<br />

takes them back to the homepage, which is where you want to go in your feature to get<br />

back to the projects listing. To add this link, put it into app/views/layouts/application<br />

.html.erb (as shown in the following listing) so that it’s available on every page, just<br />

above the .<br />

Listing 5.11 app/views/layouts/application.html.erb<br />

<br />

<br />

The call to yield should be used only once. If you put then the content<br />

of the page would be rendered twice.

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

Saved successfully!

Ooh no, something went wrong!