首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >api资源rails路由

api资源rails路由
EN

Stack Overflow用户
提问于 2017-01-03 15:20:49
回答 2查看 1.1K关注 0票数 1

因此,我的应用程序中有一些API资源,也有一些经常资源,对于经常资源,我使用:

代码语言:javascript
复制
resources :books

然后,我可以通过except: %i(destroy new edit)only,所以工作很棒!但是,对于我的资源,我永远不会有新的/编辑操作,有时我也需要传递exceptonly选项。

我想要创造出这样的东西:

api_resources :书籍

默认情况下,如果没有新的/编辑操作,那么我该如何做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-03 16:01:25

也许是这样的?

代码语言:javascript
复制
# 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
票数 3
EN

Stack Overflow用户

发布于 2020-07-21 04:51:53

@Amree的答案无法处理嵌套资源。一项改进是:

代码语言:javascript
复制
# 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41446718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档