Skip to content

Impressionist and vanity URLs (working with models that use to_param)

Benjamin Larralde edited this page Nov 28, 2013 · 1 revision

Many sites offer vanity URLs to their users, something like www.example.com/my_custom_url. If you do you'll probably need to set to_param in your model and create a custom mechanism to find the model.

When you activate uniqueness in Impressionist, it will try to match your model ID using params[:id]. If your route doesn't have a params[:id] or if the params[:id] is not in fact an ID then the look up will fail and Impressionist will create impressions with a bogus impressionable model.

To fix this, make sure you're not using the class method impressionist in your controller but rather that you're using it directly in your action and specify the model you want to track. Also, DO NOT add :impressionable_type and :impressionable_id to the :unique array.

So to track impressions of a user profile, it could look like this:

class UsersController < ApplicationController
  # don't add impressionist here!
  # impressionist actions: [:show], unique: [:impressionable_type, :impressionable_id, :session_hash]

  def show
    @user = User.find_by_user_name!(params[:user_name])
    # we add impressionist here and explicit the tracked model
    # note that the unique options will default to this model
    impressionist @user, '', unique: [:session_hash]
  end
end