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

Ticket #7356 (closed enhancement: wontfix)

Opened 3 years ago

Last modified 3 years ago

[PATCH] Allow for cleaner routing when using model objects as parameters

Reported by: bscofield Assigned to: core
Priority: normal Milestone: 1.x
Component: ActionPack Version: edge
Severity: normal Keywords: to_param routes
Cc: bitsweat

Description

Proviso: I'm not sure whether this patch properly belongs in ActiveRecord or in ActionPack, since the bulk of the changes are in AR and the effects are most noticeable in AP...

This patch allows you to specify the field that should be returned from a model's to_param method (e.g., to_param(:title)). By default, you'll still get the id back like you always have - you'll just have the option of any other model field.

On its own, this isn't very useful. The patch also adds a little more magic to the routing code that takes advantage of the enhanced to_param. Say you have the following route defined:

map.profile 'members/:username', :controller => 'member', :action => 'profile'

Currently, you use that route with profile_url(:username => @member.username) or profile_url(@member.username) %>. With this patch, however, it's just profile_url(@member) - all the convenience you'd normally get if you were using the id field in the route, but with something much more meaningful to the end user.

The patch includes tests for the AR portion, and the AP tweak doesn't break any existing tests.

Attachments

add_argument_to_activerecord_to_param.patch (2.4 kB) - added by bscofield on 01/24/07 18:23:04.
routing_update.patch (2.0 kB) - added by bscofield on 05/24/07 20:25:20.
Revised patch - use AR::Base.attributes to determine what value (if any) to drop into new route, preventing actions with unintended side-effects

Change History

01/24/07 18:23:04 changed by bscofield

  • attachment add_argument_to_activerecord_to_param.patch added.

01/28/07 13:47:53 changed by bitsweat

  • cc set to bitsweat.
  • keywords changed from activerecord actionpack namedroute to_param to to_param routes.
  • component changed from ActiveRecord to ActionPack.

Intriguing idea. I like the end result, but not the Active Record modifications (passing a to_param argument).

05/03/07 17:38:15 changed by bscofield

OK, so after thinking about this a bit (ok, for a couple of months), here's what I've come up with. It's entirely contained within ActionPack, and is updated to work with the current edge. All existing unit tests pass, and I've added a new one to support the new functionality.

05/03/07 17:40:43 changed by bscofield

  • summary changed from [PATCH] Add parameter to ActiveRecord::Base.to_param for cleaner routing to [PATCH] Allow for cleaner routing when using model objects as parameters.

Re-titling to better represent the current functionality

05/24/07 20:25:20 changed by bscofield

  • attachment routing_update.patch added.

Revised patch - use AR::Base.attributes to determine what value (if any) to drop into new route, preventing actions with unintended side-effects

05/24/07 20:43:19 changed by bscofield

  • status changed from new to closed.
  • resolution set to wontfix.

too magical (via bitsweat), so this is destined to be a plugin