Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #812 (closed defect: fixed)

Opened 4 years ago

Last modified 2 years ago

[PATCH] Use to_param instead of to_s in url_for when parameter values respond_to? :to_param

Reported by: sam Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version: 0.10.0
Severity: normal Keywords:
Cc:

Description (Last modified by david)

This patch lets you define a to_param method on classes whose instances can be converted into URL parameters. If objects passed as parameters to url_for and link_to respond to to_param, the value of that method will be used instead of the value of the object's to_s method.

For example, you might have this class:

class Page
  def initialize(number)
    @number = number
  end
  # ...
  def to_param
    @number.to_s
  end
end

You can now use instances of Page with url_for:

class BarController < ApplicationController
  def baz
    page = Page.new(4)
    url = url_for :page => page # => "http://foo/bar/baz?page=4"
  end
end

Attachments

to_param.patch (1.7 kB) - added by sam on 03/10/05 02:43:37.
Use to_param for URL parameters when available

Change History

03/10/05 02:43:37 changed by sam

  • attachment to_param.patch added.

Use to_param for URL parameters when available

03/10/05 02:45:53 changed by sam

Ulysses has pointed out that it may be beneficial to define the following:

module ActiveRecord
  class Base
    def to_param
      self.id
    end
  end
end

Comments?

03/20/05 15:42:47 changed by david

  • status changed from new to closed.
  • resolution set to fixed.
  • description changed.