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.

Adding more posts to topics<br />

really simple. When a post is created, Rails will check for any associations with the<br />

counter_cache option set to true. It then gets the current count for this association,<br />

adds 1 to it, and saves the associated object. When a post is deleted, it will do the same<br />

thing, except instead of adding 1, it will subtract 1. This way, you’re always going to<br />

have an accurate count of the number of posts each topic has.<br />

In the app/views/forem/topics/index.html.erb file now, you’ll change this line<br />

<br />

to this:<br />

<br />

Rather than inefficiently calling posts.count for each topic, Rails will now reference<br />

the posts_count attribute for the Forem::Topic object instead, saving you many queries<br />

for the index action when you have many topics. This also means that you’ll have<br />

an accurate count of posts for your topics!<br />

You’ll commit this change now:<br />

git add .<br />

git commit -m "Add posts counter cache to topics"<br />

You’ve fixed up one of the placeholder lines in the app/views/topics/index.html.erb<br />

view now, and when you get around to adding users to your engine, you’ll fix up the<br />

second placeholder line.<br />

In this section, you’ve created an interface for creating topics and their first post,<br />

which is the first feature of your engine and a well-tested one at that. You’ve seen how<br />

to use fields_for to yet again create nested resources. You first saw that back in chapter<br />

8. You’ve also seen the :counter_cache option for belongs_to that you can use to<br />

cache the count of an association, so that you don’t have to perform another query to<br />

get that number. That’s a potential lifesaver if you’re displaying a lot of objects at once<br />

and want to know an association’s count on all of them, like in your topics view.<br />

In the next section, you’re going to add the ability to add posts to a topic, because<br />

what are topics without posts?<br />

17.6 Adding more posts to topics<br />

A forum system without a way for users to reply to topics is nearly useless. The whole<br />

point of having topics is so that users can reply to them and keep the topic of conversation<br />

going for as long as they wish!<br />

The feature that you’re about to develop will let people add these replies to existing<br />

topics. They’ll click a New Reply link on a topic, fill in the post text, and click the<br />

submit button. They should then see their post within the list of posts in the topic.<br />

Simple, really!<br />

You’ll also see here more examples of integration testing with Capybara; you’ll<br />

repeat some of the concepts, but it’s a good way of learning.<br />

You’ll start out with a new spec file called spec/integration/posts_spec.rb and begin<br />

to fill it with the content from the following listing.<br />

493

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

Saved successfully!

Ooh no, something went wrong!