ray*_*ley 0 routing ruby-on-rails nested-resources
所以在我的rails应用程序中,我有两个属于用户的资源(租赁和预订).这是我的routes.rb中的代码,用于设置嵌套路由.
map.resources :users, :has_many => :reservations, :shallow => true
map.resources :users, :has_many => :rentals, :shallow => true
map.resources :rentals, :only => [:index]
map.resources :reservations, :only => [:index]
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法来做到这一点.我做了一些谷歌搜索,但我找不到一个明确的答案.
提前致谢.
-射线
正如您可以通过运行看到的那样,您的方法会复制用户的路由rake routes.您可以通过将块传递给map.resources:
map.resources :users, :shallow => true do |user|
user.resources :reservations
user.resources :rentals
end
Run Code Online (Sandbox Code Playgroud)
创建的嵌套路由将假定您始终希望以嵌套方式访问这些资源.
如果您确实需要所有已定义的路线(包括非嵌套租赁和预订索引),则需要添加:
map.resources :rentals, :only => [:index]
map.resources :reservations, :only => [:index]
Run Code Online (Sandbox Code Playgroud)
而且我不知道DRYer这样做的方法.
| 归档时间: |
|
| 查看次数: |
469 次 |
| 最近记录: |