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.

Creating tags<br />

side of the association. A join table’s sole purpose is to<br />

join together the two tables whose keys it has. In this<br />

case, the two tables are the tickets and tags tables. As<br />

you move forward in developing this association, note<br />

that, for all intents and purposes, has_and_belongs_to<br />

_many works like a two-way has_many.<br />

Figure 11.1 The tag box<br />

You’ll create two ways to add tags to a ticket. A text<br />

field for new tickets beneath the ticket’s description field will allow users to add multiple<br />

tags by using a space to separate different tags, as shown in figure 11.1.<br />

Additional tags may also be added on a comment, with a text field similar to the<br />

one from the new ticket page providing the tagging mechanism. When a ticket is created,<br />

you’ll show these tags underneath the description, as shown in figure 11.2.<br />

When a user clicks a tag, they’ll be taken to a page where they can see all tickets<br />

with that particular tag. Alternatively, if the user clicks the little “x” next to the tag,<br />

that tag will be removed from the ticket. The actions of adding and removing a tag are<br />

both actions you’ll add to your permission checking.<br />

Finally, you’ll implement a way to search for tickets<br />

that match a state, a tag, or both, by using a gem called<br />

searcher. The query will look like tag:iteration_1<br />

state: open.<br />

That’s all there is to this chapter! You’ll be adding<br />

tags to Ticketee, which will allow you to easily group and<br />

sort tickets. Let’s dig into your first feature, adding tags Figure 11.2 A tag for a ticket<br />

to a new ticket.<br />

11.1 Creating tags<br />

Tags in this application will be extremely useful for making similar tickets easy to find<br />

and manage. In this section, you’ll create the interface for adding tags to a new ticket<br />

by adding a new field to the new ticket page and defining a has_and_belongs<br />

_to_many association between the Ticket model and the not-yet-existent Tag model.<br />

11.1.1 Creating tags feature<br />

You’re going to add a text field beneath the description field on the new ticket page<br />

for this feature, as you saw earlier in figure 11.1.<br />

The words you enter into this field will become the tags for this ticket, and you<br />

should see them on the ticket page. At the bottom of features/creating_tickets.feature,<br />

you add a scenario that creates a new ticket with tags, as shown in listing 11.1.<br />

Listing 11.1 features/creating_tickets.feature<br />

Scenario: Creating a ticket with tags<br />

When I fill in "Title" with "Non-standards compliance"<br />

And I fill in "Description" with "My pages are ugly!"<br />

And I fill in "Tags" with "browser visual"<br />

287

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

Saved successfully!

Ooh no, something went wrong!