【问题标题】:Rails json request from RESTful api how to render for view来自RESTful api的Rails json请求如何呈现以供查看
【发布时间】:2013-04-27 02:32:20
【问题描述】:

我有另一个提供 RESTful JSON API 的 rails 应用程序。我可以通过下面的 curl 请求测试响应(您还可以看到它返回的第一个项目的示例)。

我可以弄清楚如何从较大的响应中提取某些项目。我只是不确定我应该如何在将其呈现给客户端的 rails 应用程序上提供响应。

我假设我需要将其保存在某种类中。我可以使用卷曲吗?我对rails非常熟悉,非常感谢任何帮助。

谢谢!

curl "http://api.foobar.com/stuff/stuff_name/catalog_items.json" -H "X-Api-Key: georgesbush"

返回一组不错的 JSON 数据。

{
    "data": {
        "catalog_items": [
            {
                "current_price": "9999.0",
                "close_date": "2013-05-14T16:08:00-04:00",
                "open_date": "2013-04-24T11:00:00-04:00",
                "stuff_count": 82,
                "minimum_price": "590000.0",
                "id": 337478,
                "estimated_price": "50000.0",
                "name": "This is a really cool name",
                "current_winner_id": 696969,
                "images": [
                    {
                        "thumb_url": "http://foobar.com/images/93695/thumb.png?1365714300",
                        "detail_url": "http://foobar.com/images/93695/detail.png?1365714300",
                        "position": 1
                    },
                    {
                        "thumb_url": "http://foobar.com/images/95090/thumb.jpg?1366813823",
                        "detail_url": "http://foobar.com/images/95090/detail.jpg?1366813823",
                        "position": 2
                    }
                ]
            }
        ]
    },
    "pagination": {
        "per_page": 1,
        "page": 1,
        "total_pages": 131,
        "total_objects": 131
    }
}

【问题讨论】:

  • 如果您使用的是 Backbone 等框架,您可以直接使用此输出并在客户端呈现您的视图。
  • 不幸的是两边都有导轨:-(

标签: ruby-on-rails json api rest


【解决方案1】:

我使用了 ruby​​ 路边库,哇,太容易了。在我的 gem 文件中添加了限制,在我的控制器操作中添加了以下代码:

http = Curl.get("http://api.foobar.com/stuff/stuff_name/catalog_items.json?per_page=1&page=1") do|http|
  http.headers['X-Api-Key'] = 'georgebush'
end
foobar =  http.body_str
@foobar = ActiveSupport::JSON.decode(foobar ).symbolize_keys

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2017-08-11
    • 2018-06-15
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多