render text,html,json

0
视频
0
完成
0%
进度
0
分钟
0
完成
0%
进度

在这个 index 方法里,没有特别指定要使用的选项,它就相当于是 render template: .. 使用一个模板文件来渲染 .. 如果你想直接响应一个纯文本 ..

可以使用 render plain: ... 加上要响应的文字 ..

render plain: 'hello'

浏览器上显示的就是响应回来的纯文字 . 打开浏览器的控制台 .. Network ... 刷新 ... 勾选一下 disable cache,禁用缓存 ...

选中这个 photos ... 看一下 Response Headers .. 表示服务器响应回来的头部信息 ... 注意它的 Content-Type 的值,现在是 text/plain ,表示响应回来的内容是纯文本 ..

响应的内容类型也可以是 html .. render html: ... 一组 html 标签,包装一点文字 ... 后面再用一下 html_safe 这个方法 ..

回到浏览器 .. 页面上显示的内容就是使用指定的 html 包装的文字 .. 再看一下这个请求的 Response Headers ,它的内容类型现在会是 text/html ...

再试一下响应 json 格式的内容 ...

render json: { "title": "photos" }

现在这个请求的 Response Headers 的内容类型会是 application/json ..

render text,html,json《 Rails:视图 》

统计

14696
分钟
0
你学会了
0%
完成

社会化网络

关于

微信订阅号

扫描微信二维码关注宁皓网,每天进步一点