27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

250 CHAPTER 10 Tracking state<br />

end<br />

@ticket = Ticket.find(params[:ticket_id])<br />

end<br />

In this action you use the template option of render when your @comment.save<br />

returns false to render a template of another controller. Previously you’ve used the<br />

action option to render templates that are for the current controller. By doing this,<br />

the @ticket and @comment objects will be available when the app/views/tickets/<br />

show.html.erb template is rendered.<br />

If the object saves successfully you redirect back to the ticket’s page by passing an<br />

Array argument to redirect_to B, which compiles the path from the arguments<br />

passed in, like form_for does to a nested route similar to /projects/1/tickets/2.<br />

But if the object doesn’t save successfully you want it to render the template that<br />

TicketsController’s show action renders. You can do this by using the render<br />

method and passing it "tickets/show" C. Keep in mind that the render method<br />

doesn’t call the action, and so any code within the show method of TicketsController<br />

wouldn’t be run. This is fine, though, because you’re setting up the @ticket variable<br />

the template renders by using the find_ticket before filter in your controller.<br />

By creating the controller, you’ve now got all the important parts needed to create<br />

comments. Let’s run this feature again by running bin/cucumber features/creating<br />

_comments.feature to see how you’re progressing. You see that it’s able to create the<br />

comment but it’s unable to find the text within the #comments element on the page:<br />

Then I should see "Added a comment!" within "#comments"<br />

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

This step is failing because you haven’t added the element with the id attribute of<br />

comments to the show template yet. This element will contain all the comments for a<br />

ticket. Let’s add it by entering the code from the following listing above the spot<br />

where you render the comment form partial.<br />

Listing 10.7 app/views/tickets/show.html.erb<br />

Comments<br />

<br />

<br />

<br />

<br />

There are no comments for this ticket.<br />

<br />

<br />

Here you create the element the scenario requires: one with an id attribute of<br />

comments. In this you check if there are no comments by using the exists? method<br />

from Active Record. This will do a light query similar to this to check if there are any<br />

comments:<br />

SELECT "comments"."id" FROM "comments"<br />

WHERE ("comments".ticket_id = 1) LIMIT 1

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

Saved successfully!

Ooh no, something went wrong!