“Banners” = Bad in Ad Blocker World

Just ran into a rather aggravating issue…

In building a site meant to preview mobile ads, I used the term “banners” in the URL to point to image previews then spent 2+ hours tracking down why it worked in my development environment but not in production.

Luckily a colleague pointed out my ad blocker, which seems to have a broad match on the word “banner”…block all. Changing the name in the path fixed the problem.


Installing Nokogiri via Bundler on Mac (or why did this take two hours?)

If this post saves one person the headache of several hours of trying to build Nokogiri via Bundler on a Mac, maybe my time wasn’t wasted, but it was painful.

The setup…
Trying to install Nokogiri 1.6.1 as a dependency for other gems via Capistrano’s bundle:install task, I continually ran into “Failed to build gem native extension” error. Scrolling through the errors, the one item that stuck out was “You have to install development tools first.”

Although 100% certain my server had Xcode Command Line tools installed (it was recently updated at noted in the App Store), I reinstalled manually and tried to deploy again. Same error.

I then proceeded down the path of using Homebrew to install the various Nokogiri dependencies (e.g. libxml2, libxslt). Deploy…build…fail.

I ran “gem install nokogiri” locally on the server. Build…success. Tried to deploy again…build…fail.

Finally, I dug up the make log for Nokogiri (path below for reference) where I noted a message indicating that “gcc-4.2” was not installed. gcc is installed, but the build script seems specific. Not finding a switch to change the name of gcc, I went brute force:

sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

Deploy…build…success! Annoying problem, solved.

Better suggestions welcome.

Make log: /shared/bundle/ruby/2.0.0/gems/nokogiri-1.6.1/ports/x86_64-apple-darwin13.1.0/ext/nokogiri/mkmf.log

After all that, I noticed a warning: “Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.9.1”. After trying a few paths, I was able to rectify the warning via the steps in the comments of this :

  1. brew remove –force libxml2
  2. bundle config –delete build.nokogiri
  3. bundle exec gem uninstall nokogiri libxml-ruby
  4. bundle
  5. The commenter notes that Nokogiri 1.6+ now bundle the necessary libraries so there is no need to use the Homebrew versions.

Ransack: Search with Multiple Checkboxes (Rails)

I’ve been using the outstanding Ransack gem to enable search for a recent project. Ransack provides a tremendous amount of flexibility in a straightforward manner, but it took me a bit to understand how to search a single field across multiple values selected via checkboxes. My approach follows.

(Note that the field I was searching contained a serialized array of data, not to make life more difficult)

To start, a simplified model:

class Zoo < ActiveRecord::Base
  has_many :animals

class Animal < ActiveRecord::Base
  belongs_to :zoo # we'll assume an animal can only belong to one zoo
  validates_presence_of :name

Next, the controller, which is pretty much out-of-the-box from the Ransack documentation:

class ZoosController < ApplicationController
  def search # or index
    @search = Zoo.search(params[:q])
    @zoos = @search.result(distinct: true)

And finally, the magic is really in the view:

<%= search_form_for @search, url: search_zoos_url do |f| %>
  <% Animal.all.uniq.each do |animal| %>
    # the check_box helper will not work as effectively here
    <%= check_box_tag('q[animals_name_cont_any][]', animal.name) %>
    <%= animal.name %>
  <% end %>
  <%= f.submit "Search" %>
<% end %>

The key is the name of the check box field. We are passing an array and also using Ransack’s cont_any predicate, which searches for any records whose name field contains one of the selected names.