簡體   English   中英

api 資源 rails 路線

[英]api resource rails routes

所以我的應用程序中有一些 API 資源和一些常規資源,對於常規資源,我使用:

resources :books

然后我可以通過except: %i(destroy new edit)或者only這樣效果很好! 但是,對於我的資源,我永遠不會有新的/編輯操作,有時我也需要通過exceptonly選項。

我正在考慮創建類似的東西:

api_resources :書籍

默認情況下沒有新建/編輯操作,我該怎么做?

也許是這樣的?

# config/routes.rb
Rails.application.routes.draw do
  def api_resources(res)
    resources res, only: [:new, :edit]
  end

  api_resources :a
  api_resources :b
end

# output
Prefix Verb URI Pattern           Controller#Action
 new_a GET  /a/new(.:format)      a#new
edit_a GET  /a/:id/edit(.:format) a#edit
 new_b GET  /b/new(.:format)      b#new
edit_b GET  /b/:id/edit(.:format) b#edit

@Amree 的回答無法處理嵌套資源。 一個改進是:

# config/routes.rb
Rails.application.routes.draw do
  def editable_resoustrong textrces(res, &block)
    resources res, only: %i[new edit], &block
  end

  editable_resources :a
end

# output
Prefix Verb URI Pattern           Controller#Action
 new_a GET  /a/new(.:format)      a#new
edit_a GET  /a/:id/edit(.:format) a#edit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM