将 URL 转换为 JSON 版本? [英] Converting URL to JSON version?

查看:34
本文介绍了将 URL 转换为 JSON 版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序的所有页面上,我想要一个指向当前页面的 JSON 版本的链接.有什么巧妙的技巧可以做到这一点?变得复杂的地方是当额外的&"参数包含在 URL 中.

On all the pages of my app, I want a link to the JSON version of current page. Any neat tricks to do this? Where it got complicated was when additional '&' parameters were included in the URL.

所以网址将被转置为:

'/users' => '/users.json'

'/users?page=1&per_page=5' => '/users.json?page=1&per_page=5'

推荐答案

此答案的署名来自 获取当前页面的 url,但使用不同的格式,并进行修改:

Attribution for this answer from Get url for current page, but with a different format, with modifications:

助手:

def current_url(new_params)
 url_for params.merge(new_params)
end

链接:

<%= link_to "JSON of this page", current_url(:format=>:json)

这篇关于将 URL 转换为 JSON 版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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