【问题标题】:rails routing nested resourcesrails 路由嵌套资源
【发布时间】:2012-01-27 09:31:55
【问题描述】:

我有下一个资源

 resources :countries do
    resources :cities
 end    

 resources :cities do
    resources :streets
 end

它生成下一条路线

GET    /countries/:country_id/cities(.:format)                          cities#index
POST   /countries/:country_id/cities(.:format)                          cities#create
new_country_city GET    /countries/:country_id/cities/new(.:format)                      cities#new
edit_country_city GET    /countries/:country_id/cities/:id/edit(.:format)                 cities#edit
GET    /countries/:country_id/cities/:id(.:format)                      cities#show
PUT    /countries/:country_id/cities/:id(.:format)                      cities#update
DELETE /countries/:country_id/cities/:id(.:format)                      cities#destroy


......
cities GET    /cities(.:format)                                                cities#index
POST   /cities(.:format)                                                cities#create
new_city GET    /cities/new(.:format)                                            cities#new
edit_city GET    /cities/:id/edit(.:format)                                       cities#edit
city GET    /cities/:id(.:format)                                            cities#show
PUT    /cities/:id(.:format)                                            cities#update
DELETE /cities/:id(.:format)                                            cities#destroy

我不想访问没有国家/地区 ID 的城市,但我也不想使用 3 级嵌套资源,所以我可以像 next 一样更改路线

 resources :countries do
        resources :cities
     end    

     resources :cities, :except => [:index, :destroy, :edit, :show, :create, :new, :update] do
        resources :streets
     end

是否有某种快捷方式可以禁用所有操作,而不是在 :except 选项处写入所有默认操作????

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 routes url-routing


    【解决方案1】:
    resources :cities, :only => [] do
        ...
    end
    

    【讨论】:

    • 在 Rails 2.2 中,您可以使用 :except => :all 但这不再有效。谢谢你。
    【解决方案2】:

    你可以按照这条路线走

    资源:主题做 资源:解决方案 结尾 资源:解决方案,仅:[] 做 资源 :reviews,除了:[:show, :index] 结尾

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-05
      • 2011-05-23
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2015-06-30
      相关资源
      最近更新 更多