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.

300 CHAPTER 11 Tagging<br />

<br />

true,<br />

:url => remove_ticket_tag_path(@ticket, tag),<br />

:method => :delete,<br />

:html => { :id => "delete-#{tag.name.parameterize}" } %><br />

<br />

<br />

Here, you check that a user can tag in the ticket’s project. If they can’t tag, then you<br />

won’t show the X to remove the tag. This is to prevent everyone from removing tags as<br />

they feel like it. Remember? With great power comes great responsibility.<br />

You use the :remote option for the link_to, to indicate to Rails that you want this<br />

link to be an asynchronous request. This is similar to the Add Another File button you<br />

provided in chapter 9, except this time you don’t need to call out to any JavaScript to<br />

determine anything, you only need to make a request to a specific URL.<br />

For the :url option here, you pass through the @ticket object to remove_ticket<br />

_tag_path so that your action knows what ticket to delete the tag from. Remember:<br />

your primary concern right now is disassociating a tag and a ticket, not completely<br />

deleting the tag.<br />

Because this is a destructive action, you use the :delete method. You’ve used this<br />

previously for calling destroy actions, but the :delete method is not exclusive to the<br />

destroy action, and so you can use it here as well.<br />

The final option, :html, lets you define HTML attributes for the link. Inside this<br />

hash, you set the id key to be the word delete, followed by a hyphen and then the name<br />

of your tag parameterize’d. For the tag in your scenario, this is the id that you’ll use<br />

to click this link. Capybara supports following links by their internal text, the name<br />

attribute, or the id attribute.<br />

When you run your feature with bundle exec cucumber features/deleting_tags<br />

.feature, you see that it reports the same error message at the bottom:<br />

When I follow "delete-this-tag-must-die"<br />

no link with title, id or text 'delete-this-tag-must-die'<br />

Ah! A quick eye would have spotted an error when the browser launched by Web-<br />

Driver tried going to this page; it looks like figure 11.5.<br />

This error is coming up because you haven’t defined the route to the remove<br />

action yet. You can define this route in config/routes.rb inside the resources :tickets<br />

block, morphing it into this:<br />

Figure 11.5 Internal Server Error

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

Saved successfully!

Ooh no, something went wrong!