Permalink
Browse files

AS guide: explains to_param

  • Loading branch information...
1 parent 0c70bf3 commit c35d55c918159b62669a481b0c6f94bb7a7aefaa @fxn fxn committed Jun 13, 2009
Showing with 36 additions and 0 deletions.
  1. +36 −0 railties/guides/source/active_support_overview.textile
@@ -96,6 +96,42 @@ some_klass.acts_like?(:string)
Rails has classes that act like +Date+ or +Time+ and follow this contract.
+h4. +to_param+
+
+All objects in Rails respond to the method +to_param+, which is meant to return a value that represents them as value in a query string, or as a URL fragment.
+
+By default +to_param+ just calls +to_s+:
+
+<ruby>
+7.to_param # => "7"
+</ruby>
+
+and some classes in Rails overwrite it.
+
+For example +nil+, +true+, and +false+ return themselves. +Array#to_param+ calls +to_param+ on the elements and joins the result with "/":
+
+<ruby>
+[0, true, String].to_param # => "0/true/String"
+</ruby>
+
+Notably, the Rails routing system calls +to_param+ on models to get a value for the +:id+ placeholder. +ActiveRecord::Base#to_param+ returns the +id+ of a model, but you can redefine that method in your models. For example, given
+
+<ruby>
+class User
+ def to_param
+ "#{id}-#{name.parameterize}"
+ end
+end
+</ruby>
+
+you get:
+
+<ruby>
+user_path(@user) # => "/users/357-john-smith"
+</ruby>
+
+WARNING. Controllers need to be aware of any redifinition of +to_param+ because when a request like that comes in "357-john-smith" is the value of +params[:id]+.
+
h3. Extensions to +Module+
...

0 comments on commit c35d55c

Please sign in to comment.