因此,我的应用程序中有一些API资源,也有一些经常资源,对于经常资源,我使用:
resources :books然后,我可以通过except: %i(destroy new edit)或only,所以工作很棒!但是,对于我的资源,我永远不会有新的/编辑操作,有时我也需要传递except和only选项。
我想要创造出这样的东西:
api_resources :书籍
默认情况下,如果没有新的/编辑操作,那么我该如何做呢?
发布于 2017-01-03 16:01:25
也许是这样的?
# 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发布于 2020-07-21 04:51:53
@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#edithttps://stackoverflow.com/questions/41446718
复制相似问题