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.

288 CHAPTER 11 Tagging<br />

And I press "Create Ticket"<br />

Then I should see "Ticket has been created."<br />

And I should see "browser" within "#ticket #tags"<br />

And I should see "visual" within "#ticket #tags"<br />

When you run the “Creating a ticket with tags” scenario using bin/cucumber features<br />

/creating_tickets.feature:50 it will fail, declaring that it can’t find the Tags field.<br />

Good! It’s not there yet:<br />

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

cannot fill in, no text field, text area or password field<br />

with id, name, or label 'Tags' found (Capybara::ElementNotFound)<br />

You’re going to take the data from this field, process each word into a new Tag object,<br />

and then link the tags to the ticket when it’s created. You’ll use a text_field_tag to<br />

render the Tags field this way. text_field_tag is similar to a text_field tag, but it<br />

doesn’t have to relate to any specific object like text_field does. Instead, it will output<br />

an input tag with the type attribute set to text and the name set to whatever<br />

name you give it.<br />

11.1.2 Using text_field_tag<br />

To define this field, you put the following code underneath the p tag for the description<br />

in app/views/tickets/_form.html.erb:<br />

<br />

<br />

<br />

<br />

This field will be sent through to TicketsController as params[:tags], rather than<br />

the kind of attributes you’re used to, such as params[:ticket][:title].<br />

By specifying params[:tags] as the second argument to text_field_tag, you can<br />

re-populate this field when the ticket cannot be created due to it failing validation.<br />

When you re-run this scenario again with bin/cucumber features/creating<br />

_tickets.feature:51, it no longer complains about the missing Tags field, telling<br />

you instead that it can’t find the tags displayed on your ticket:<br />

And I should see "browser" within "#ticket #tags"<br />

Unable to find css "#ticket #tags" (Capybara::ElementNotFound)<br />

You now need to define this #tags element inside the #ticket element on the ticket’s<br />

page so that this part of the scenario will pass. This element will contain the tags for<br />

your ticket, which your scenario will assert are actually visible.<br />

11.1.3 Showing tags<br />

You can add this new element, with its id attribute set to tags, to app/views/tickets/<br />

show.html.erb by adding this simple line underneath where you render the ticket’s<br />

description:<br />

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

Saved successfully!

Ooh no, something went wrong!