Rails 将所有路由默认为 format: :json [英] Rails make all routes default to format: :json
本文介绍了Rails 将所有路由默认为 format: :json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你能把所有路由默认为 json
吗?
Can you make all routes default to json
?
对于 api 范围,我有以下内容,但想知道您是否可以对全局范围执行相同的操作?
I have the following for an api scope but am wondering if you can do the same for the global scope?
scope :api, defaults: {format: :json} do
get "/search(/:query)(/:location)" => "search#index"
end
例如所有 user
资源也将默认为 json
For example all user
resources would also default to json
resources :users
推荐答案
使用约束
Use constraints
constraints format: :json do
resources :users
end
或
resources :users, :defaults => { :format => 'json' }
这篇关于Rails 将所有路由默认为 format: :json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文