Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

The simplest and most pointless rails plugin ever.

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 README
Octocat-spinner-32 init.rb
README
Find-Param
==========

This is a simple plugin to let you easily define a parameter to use for
#to_param, and also define a finder to access that same parameter.

This plugin is most useful for situations where you want to override the usage
of ID as the default parameter value. Using a slug creates URLs that can be
friendlier for humans and bots alike.


Getting Started
---------------

Here we define the find param to be a slug:

  class Post < ActiveRecord::Base
    find_param :slug
  end

This will set the #to_param to be #slug and add a finder to the model called
#find_by_param that can be used in your controllers to fetch records using
params[:id].

  class PostController
    before_filter :find_post, :only => %(show edit update destroy)

    def find_post
      find_by_param(params[:id])
    end
  end


Options
-------

To have your find param automatically populated on record creation, use the
:initialize_with option.

  class Post < ActiveRecord::Base
    find_param :slug, :initialize_with => :title
  end

This will use the title property to populate the slug. By default this value is
lowercased and all whitespace and special characters are replaced by hyphens.
To specify your own formatted for the find param, there a :using option that
accepts a proc.

  class Post < ActiveRecord::Base
    find_param :slug, :initialize_with => :title, :using => Proc.new { |value| value.upcase }
  end

You can also pass :raise_on_not_found => true to have it raise an
ActiveRecord::RecordNotFound erorr when the result set is empty.
Something went wrong with that request. Please try again.