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.

440 CHAPTER 16 Basic performance enhancements<br />

1 scenario (1 passed)<br />

10 steps (10 passed)<br />

That’s all there is to paginating a resource. You can also call the page and per methods<br />

on models themselves rather than associations; it was just in this case that you<br />

were calling it on an association.<br />

Before you make a commit for this change, quickly make sure that everything’s<br />

working by running rake cucumber:ok spec:<br />

Failing Scenarios:<br />

cucumber features/searching.feature:23<br />

cucumber features/searching.feature:29<br />

cucumber features/searching.feature:35<br />

62 scenarios (3 failed, 59 passed)<br />

736 steps (3 failed, 6 skipped, 727 passed)<br />

Oh dear, it appears the feature in features/searching.feature has been broken by your<br />

changes! Good thing that you’ve got a feature to catch these kinds of things.<br />

FIXING BROKEN SCENARIOS<br />

All three scenarios in this feature failed with the same error:<br />

undefined method 'current_page' for ...<br />

This looks to be associated with the feature you just implemented, as it’s trying to call<br />

a method called current_page. If you look a couple of lines down in the output, you’ll<br />

see that there’s a line in the stack trace that shows that this is from Kaminari:<br />

...kaminari/helpers/action_view_extension.rb:21:in 'paginate'<br />

Okay, so it looks to be a problem coming from Kaminari, but why? Well, if you look<br />

even further down in the stacktrace for this error somewhere in your application,<br />

probably from the app folder, you’ll come across this line:<br />

./app/controllers/tickets_controller.rb:60:in 'search'<br />

What’s so great about this line? Well, this line renders the projects/show view:<br />

render "projects/show"<br />

Above that, however, is the real culprit:<br />

@tickets = @project.tickets.search(params[:search])<br />

You’re not calling either page or per on your search results, and so it’s not going to be<br />

paginating them. You’re going to call the same methods you called back in the<br />

ProjectsController’s show action here so that you get paginated search results:<br />

@tickets = @project.tickets.search(params[:search])<br />

@tickets = @tickets.page(params[:page]).per(50)<br />

With paginated search results, the feature in features/searching.feature will no longer<br />

complain when you run it with bin/cucumber features/searching.feature:<br />

3 scenarios (3 passed)<br />

39 steps (39 passed)

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

Saved successfully!

Ooh no, something went wrong!