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.

Leaving a comment<br />

It only selects the id column from the comments table and limits the result set to 1,<br />

which results in a super-fast query to check if there are any comments. You used<br />

exists? back in chapter 8 when you checked if a ticket had any assets. You could use<br />

empty? here instead, but that would load the comments association in its entirety and<br />

then check to see if the array was empty. If there were a lot of comments, then this<br />

would be slow. By using exists?, you stop this potential performance issue from cropping<br />

up.<br />

Inside this div, if there are comments, you call render and pass it the argument of<br />

@ticket.comments. On the end of that call, select on it.<br />

You use select here because you don’t want to render the comment object you’re<br />

building for the form at the bottom of the page. If you left off the select,<br />

@ticket.comments would include this new object and render a blank comment box.<br />

When you call select on an array, you can pass it a block that it will evaluate on all<br />

objects inside that array and return any element that makes the block evaluate to anything<br />

that’s not nil or false.<br />

The argument you pass to select is called a Symbol-to-Proc and is a shorter way of<br />

writing this:<br />

{ |x| x.persisted? }<br />

This is a new syntax versions of Ruby >= 1.8.7 and used to be in Active Support in Rails<br />

2. It’s a handy way of writing a shorter block syntax if you’re only looking to call a single<br />

method on an object.<br />

The persisted? method checks if an object is persisted in the database by checking<br />

if it has its id attribute set and will return true if that’s the case and false if not.<br />

By using render in this form, Rails will render a partial for every single element in<br />

this collection and will try to locate the partial using the first object’s class name.<br />

Objects in this particular collection are of the Comment class, so the partial Rails will try<br />

to find will be at app/views/comments/_comment.html.erb, but you don’t have this file<br />

right now. Let’s create it and fill it with the content from the following listing.<br />

Listing 10.8 app/views/comments/_comment.html.erb<br />

<br />

<br />

<br />

<br />

Here you’ve used a new method, div_for. This method generates a div tag around<br />

the content in the block and also sets class and id attributes based on the object<br />

passed in. In this instance, the div tag would be the following:<br />

<br />

The class method from this tag is used to style your<br />

comments so that they will look like figure 10.3 when<br />

the styles from the stylesheet are applied.<br />

Figure 10.3 A comment<br />

251

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

Saved successfully!

Ooh no, something went wrong!