Rails Bossman Gem

— September 17, 2008 at 10:49 CDT || By: Johnson, Chris


So I recently needed to implement a site wide search and happened to find this ruby gem at the right time (http://github.com/jpignata/bossman-gem/tree/master). However I was required to set a few things.  I also had to figure out the string needed to pass to the search, if you want to search only one domain. Below are the steps I took to implement this into a rails application.

First you need to add the git hub sources and  install the gem

gem sources -a http://gems.github.com
gem install jpignata-bossman


Next you'll want to create a new initializer, I called mine boss.rb and put this code in it:

require 'bossman'
include BOSSMan
BOSSMan.application_id = "your_yahoo_api_key"


Next you'll want to create a search controller in your application, then you will want put this code in the controller for your action that will handle the results.

@search = BOSSMan::Search.web("http://www.yoursite.com + #{params[:your_form][:your_search_field]}", 0, 50)

The 50 is the number of results that you want back.  I do not store the search results or do any pagination in this example.

Lastly you'll want to code up your views.  Here is a sample of my results view and my search form:

*** Search Form ******

<% form_tag :action => "search_results" do %>
<p>
  <%= label :search_form, :find %>:
  <%= text_field :search_form, :find %>
</p>
    <p>
        <%= submit_tag 'Search' %>
    </p>
<% end %>

*** Results View *****

    <h3>Results</h3>
    <% if @search.count.to_i > 0 %>
        <% @search.results.each do |result| %>
            <p>
                 <b><%= link_to result.title, result.url %></b><br/>
                <%= result.abstract %><br />
            </p>   
        <% end %>
    <% else %>
        No results found
    <% end %>

1 comment — rails, how to, gems, development

Comments
  1. Amit2009-11-23 02:34:22

    wat about pagination ? i'm trying hard for this.

Add Comment



Comments are styled using Markdown.