3
我将一个Merb应用程序移植到Rails 3.在Merb中,我们可以在路径周围放置一个Identify块以定义如何提供:id路径参数例如,定义:路径中的id不是轨道中的id 3
# 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 "https://stackoverflow.com/users/:id", :action => "show", :as => "show_user"
get "https://stackoverflow.com/users/:id/edit", :action => "edit", :as => "edit_user"
put "https://stackoverflow.com/users/:id", :action => "update", :as => "update_user"
post "/users", :action => "create", :as => "create_user"
end
在Rails,如Merb的,您可以覆盖to_param提供价值路线的替代ID,但要使用你想使用一个id,另一次一次的情况下,对同一物体采用不同的方法(如上所述),识别方便。什么是Rails 3的等价物?我查看了Rails 3源代码并进行了测试,结果没有看到与Identify等价的任何内容。我错过了吗?
我可以重构的东西,也许应该不需要它在这种情况下,但我仍然想知道如果我错过了什么。
谢谢。
我很好奇,如果你曾经找到一个很好的方法来实现这个功能。 – dmkash 2011-07-04 21:32:42