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.

into these:<br />

resources :projects do<br />

resources :tickets do<br />

collection do<br />

get :search<br />

end<br />

end<br />

end<br />

Finding tags<br />

The collection block here defines that there’s a search action that may act on a collection<br />

of tickets. This search action will receive the parameters passed through from<br />

the form_tag you have set up. When you run your feature again by using bin/cucumber<br />

features/searching.feature, you see that it’s reporting that the search action is<br />

missing:<br />

And I press "Search"<br />

The action 'search' could not be found for TicketsController<br />

Good! The job of this action is to find all the tickets that match the criteria passed in<br />

from the form as params[:search], which is what you can use the Searcher gem for.<br />

11.5.2 Searching by state with Searcher<br />

The Searcher gem provides the functionality of parsing the labels in a query such as<br />

tag:iteration_1 and determines how to go about finding the records that match the<br />

query. Rather than working like Google, where you could put in iteration_1 and it<br />

would know, you have to tell it what iteration_1 means by prefixing it with tag:. You<br />

use this query with the search method provided by Searcher on a configured model,<br />

and it will return only the records that match it:<br />

Ticket.search("tag:iteration_1")<br />

You’ll use this method in the search action for TicketsController in a bit.<br />

The first port of call to begin to use the Searcher gem is to add it to your Gemfile<br />

underneath gem 'paperclip':<br />

gem 'searcher'<br />

To install this gem, you run bundle install. Now for the configuration. Searcher is<br />

configured by a searcher call in a class, just as associations are set up by using<br />

has_many and friends. In app/models/ticket.rb directly above 5 the first belongs_to,<br />

put this code:<br />

searcher do<br />

label :tag, :from => :tags, :field => :name<br />

end<br />

5 Code from gems or plugins should go above any code for your models, because it may modify the behavior<br />

of the code that follows it.<br />

305

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

Saved successfully!

Ooh no, something went wrong!