tylerhunt / find-param forked from jseifer/find-param
- Source
- Commits
- Network (1)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
56089a2
Tyler Hunt (author)
Thu May 29 13:35:15 -0700 2008
commit 56089a231925226bd9bbd04c4a104b5533430fcd
tree 64c0d84dff5fe947b09d0ee9b07ff9a7f6ff62ea
parent 60c17dfec0c579c58fe9df045cbab87e33651d6f
tree 64c0d84dff5fe947b09d0ee9b07ff9a7f6ff62ea
parent 60c17dfec0c579c58fe9df045cbab87e33651d6f
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.

