law*_*nce 1 ruby-on-rails url-routing
这里是我的困境:我有两种类型的语义完全不同的路线,并应该去不同的控制器.
ny/new-york/brooklyn/cleaners # should go to a list of cleaners for a neighborhood
ny/new-york/cleaners/mrclean # should go to an individual cleaner's page
Run Code Online (Sandbox Code Playgroud)
请注意,这里的"brooklyn"和"cleaners"仅仅是示例.该应用程序有许多服务类型(例如"清洁")和许多邻域,因此不可能将列表硬编码到正则表达式中并使用它来区分这两个路径.
是否有可能在路由决策中涉及访问ActiveRecord模型的任意方法?我正在使用Rails 2.3.8.
编辑:动态服务的新答案
查看此博客条目似乎可以在路由中使用ActiveRecords.
也许你可以这样做:
service_names = Service.all.map(&:short_name) # assuming the property 'short_name' is the value used in urls
service_names.each do |service_name|
map.connect ':state/:city/#{service_name}/:company' :controller => ‘company’, :action => ‘show’ # to show the company's page
map.connect ':state/:city/:neighborhood/#{service_name}_finder' :controller => ‘company_finder’, :action => ‘find’ # to list the companies for the given service in a neighborhood
end
Run Code Online (Sandbox Code Playgroud)
这应该仍然可以防止冲突,因为某个服务的路线在邻域的路线之前
老不好的回答
你不能使用以下两条路线吗?
map.connect ':state/:city/cleaners/:cleaner' :controller => ‘cleaners’, :action => ‘show’ # to show the cleaner's page
map.connect ':state/:city/:neighborhood/cleaners' :controller => ‘cleaner_finder’, :action => ‘find’ # to list the cleaners of a neighborhood
Run Code Online (Sandbox Code Playgroud)
在您的控制器中,您应该能够使用params [:state],params [:city]等检索:state,:city和其他值.
在第一行放置:state /:city/cleaners /:cleaner应该可以防止歧义.
| 归档时间: |
|
| 查看次数: |
506 次 |
| 最近记录: |