默认呈现 JSON 而不是 HTML? [英] Render JSON instead of HTML as default?

查看:48
本文介绍了默认呈现 JSON 而不是 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图告诉 rails 3.2 它应该默认呈现 JSON,并像这样完全踢 HTML:

I try to tell rails 3.2 that it should render JSON by default, and kick HTML completely like this:

respond_to :json    

def index
  @clients = Client.all
  respond_with @clients
end

使用此语法,我必须将 .json 添加到 URL.我怎样才能实现它?

With this syntax, I have to add .json to the URL. How can I achieve it?

推荐答案

您可以修改您的 routes.rb 文件以指定默认格式

You can modify your routes.rb files to specify the default format

routes.rb

resources :clients, defaults: {format: :json}

这将修改整个clients_controller

这篇关于默认呈现 JSON 而不是 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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