如何在 Rails 中设置路由的默认格式? [英] How to set default format for routing in Rails?

查看:43
本文介绍了如何在 Rails 中设置路由的默认格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有如下路由代码:

  resources :orders, only: [:create], defaults: { format: 'json' }
  resources :users,  only: [:create, :update], defaults: { format: 'json' } 
  resources :delivery_types, only: [:index], defaults: { format: 'json' }
  resources :time_corrections, only: [:index], defaults: { format: 'json' }

是否可以使用 1 个字符串为所有资源设置默认格式,而每一行都没有默认"哈希?谢谢.

It is possible to set default format for all resources using 1 string without 'defaults' hash on each line? Thanks.

推荐答案

试试这个:

scope format: true, defaults: { format: 'json' } do
  resources :orders, only: [:create]
  resources :users,  only: [:create, :update] 
  resources :delivery_types, only: [:index]
  resources :time_corrections, only: [:index]
end

这篇关于如何在 Rails 中设置路由的默认格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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