views:

17

answers:

0

I am porting a Merb app to Rails 3. In Merb we could put an Identify block around a route to define how an :id route parameter was to be supplied, e.g.,

# this is a Merb route that I want to port to Rails 3 routing; I get everything except 
# how to replicate the behavior of Merb's Identify block which doesn't require one to 
# futz with overriding to_param on user; a user instance gets passed to the url builder
# ala url(:edit_password_reset, user) and this tells the router to use the 
# reset_password_token method on user to supply the :id value for this one route
Identify User => :reset_password_token do
  match("/reset-password/:id", :method => :get).to(:controller => "password_resets", :action => "edit").name(:edit_password_reset)
end

# and then later define more routes that use the user's id without a problem 
# since to_param was not overridden on user; here I have already translated to 
# Rails 3 and this works fine
controller :users  do
  get "/register", :action => "new", :as => "new_user"
  get "/users", :action => "index", :as => "users"
  get "/users/:id", :action => "show", :as => "show_user"
  get "/users/:id/edit", :action => "edit", :as => "edit_user"
  put "/users/:id", :action => "update", :as => "update_user"
  post  "/users", :action => "create", :as => "create_user"
end

In Rails, as in Merb, you can override to_param to provide an alternative id value for routes, but for a case where one time you want to use an id and another time you want to use a different method on the same object (as above), Identify is convenient. What is the Rails 3 equivalent? I looked through the Rails 3 source and tests and didn't see anything equivalent to Identify. Did I miss it?

I can refactor things and maybe should to not need it in this case, but still I would like to know if I missed something.

Thanks.