Rails资源路由的默认:exclude选项 [英] Default :exclude option for Rails resource routing

查看:64
本文介绍了Rails资源路由的默认:exclude选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个小问题:

我正在将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中的每个资源上具有:except 选项不是什么大不了的事/routes.rb ,但是有没有一种方法可以定义默认值,所以我不必在每个资源上都指定它?我想稍微干燥一下这段代码,而不必像在各处传递带有默认选项的局部变量那样la脚。

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 以进行救援!

with_options(except: [:new, :edit]) do |opt|
  opt.resource :session
  opt.resource :another_resource
  opt.resources :people
end

这篇关于Rails资源路由的默认:exclude选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆