Skip to content

Routing configuration

Florian Hanke edited this page Mar 28, 2011 · 16 revisions

Routing

Routes define a mapping for URL paths like /searches?query=something to Picky Search objects.

Routing is defined in app/application.rb like all other search specific options.

Example

class PickySearch < Application

  # ...

  route %r{^/books} => books_search
  root 200

end

root 200 is convenience for route %r{^/$} => an empty 200 success response

The query object will then extract and use the query string (after ?)

  • query: The query text.
  • offset: The offset, default 0.

to calculate the results.

So, sending the following url by curl to the above example server configuration

curl 'localhost:8080/books?query=test&ids=20&offset=10'

would return all result combinations for query “test” with an ids amount of 20 (20 result ids returned), and an offset of 10, which means that the first 10 results would be omitted.

route(path_mapping_hash)

It’s simple. route maps multiple (or just one) path matching regexps to queries.

class PickySearch < Application

  # ...
  route %r{^/books} => Search.new(my_beautiful_books_index)

end

This will match all URL paths beginning with /books and map the request to the query my_query.

Matching query params

route has another trick up its sleeve, it can also match a query param (just a single one):

route %r{^/books} => my_query, :some_type => :lovely_query_param

This will match only /books...?some_type=lovely_query_param (with the given query param – additional params are no problem, in fact needed, like query=something).