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.

310 CHAPTER 11 Tagging<br />

into this:<br />

"tag:#{tag.name}") %><br />

For this link_to, you use the search_project_tickets_path helper to generate a<br />

route to the search action in TicketsController for the current ticket’s project, but<br />

then you do something different. After you specify @ticket.project, you specify<br />

options.<br />

These options are passed in as additional parameters to the route. Your search<br />

form passes through the params[:search] field, and your link_to does the same<br />

thing. So you see that when you run bin/cucumber features/searching.feature<br />

:35, this new scenario will now pass:<br />

1 scenario (1 passed)<br />

13 steps (13 passed)<br />

This feature allows users to click a tag on a ticket’s page to then see all tickets that have<br />

that tag. Let’s make sure you didn’t break anything with this small change by running<br />

rake cucumber:ok spec. You should see this output:<br />

59 scenarios (59 passed)<br />

658 steps (658 passed)<br />

# and<br />

39 examples, 0 failures, 19 pending<br />

Great, nothing broke! Let’s commit this change:<br />

git add .<br />

git commit -m "Users can now click a tag's name to go to<br />

a page showing all tickets for it"<br />

git push<br />

Users are now able to search for tickets based on their state or tag, as well as go to a list<br />

of all tickets for a given tag by clicking the tag name that appears on the ticket’s page.<br />

This is the final feature you needed to implement before you have a good tagging system<br />

for your application.<br />

11.6 Summary<br />

In this chapter, we’ve covered how to use a has_and_belongs_to_many association to<br />

define a link between tickets and tags. Tickets are able to have more than one tag, but<br />

a tag is also able to have more than one ticket assigned to it, and therefore you use this<br />

type of association. A has_and_belongs_to_many could also be used to associate people<br />

and the locations they’ve been to. 6<br />

You first wrote the functionality for tagging a ticket when it was created, and then<br />

continued by letting users tag a ticket through the comment form as well.<br />

6 Like foursquare does.

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

Saved successfully!

Ooh no, something went wrong!