问题描述
一个小问题:
我正在将Rails用于我的REST API,但是由于它是RESTful API,所以我真的不需要 :new
或:edit
路由我的任何资源,因为人们只会完全通过自动JSON请求而不是图形方式与该API进行交互。例如,不需要专用的编辑页面。
I am using Rails for my REST API, but since it is a RESTful API I don't really need :new
or :edit
routes for any of my resources since people will only be interacting with this API entirely through automated JSON requests, not graphically. There's no need for a dedicated edit page, for instance.
当前,我需要对定义的每个资源执行以下操作:
Currently, I need to do something like this for every resource defined:
# routes.rb
resources :people, except: [:new, :edit]
在 / config中的每个资源上具有
,但是有没有一种方法可以定义默认值,所以我不必在每个资源上都指定它?我想稍微干燥一下这段代码,而不必像在各处传递带有默认选项的局部变量那样la脚。:except
选项不是什么大不了的事/routes.rb
It's not a big deal to have :except
options on every resource in /config/routes.rb
, but is there a way to define defaults so I don't have to specify this on every resource? I'd like to DRY up this code a bit, without doing something lame like passing around a local variable with default options everywhere.
更一般地说,您可以设置默认选项吗?使Rails路线除了:排除
?
More generally, can you set default options for Rails routes to follow aside from :exclude
?
谢谢!
推荐答案
以进行救援!
with_options(except: [:new, :edit]) do |opt|
opt.resource :session
opt.resource :another_resource
opt.resources :people
end
这篇关于Rails资源路由的默认:exclude选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!