Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (38 sloc) 1.51 KB
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
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
class PostController
before_filter :find_post, :only => %(show edit update destroy)
def find_post
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
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 => { |value| value.upcase }
You can also pass :raise_on_not_found => true to have it raise an
ActiveRecord::RecordNotFound erorr when the result set is empty.