【问题标题】:Using my Rails app API for iPhone App使用我的 Rails 应用 API for iPhone App
【发布时间】:2013-08-18 21:23:02
【问题描述】:

我正在使用 Treehouse Library 学习 iOS 开发。构建一个从 json API 获取信息的应用程序。

在 Treehouse API 页面中,所有帖子都位于名为“帖子”(http://blog.teamtreehouse.com/api/get_recent_summary/) 的父级下

posts: [
{
  id: 22198,
  url: "http://blog.teamtreehouse.com/using-github-pages-to-host-your-website",
  title: "Using GitHub Pages To Host Your Website",
  date: "2013-08-16 09:30:20",
  author: "Matt West",
  thumbnail: "http://blog.teamtreehouse.com/wp-content/uploads/2013/08/github-pages-feature-150x150.jpg"
},
{
  id: 22196,
  url: "http://blog.teamtreehouse.com/running-tests-in-ruby-on-rails-treehouse-quick-tip",
  title: "Running Tests in Ruby on Rails – Treehouse Quick Tip",
  date: "2013-08-15 14:30:48",
  author: "Jason Seifer",
  thumbnail: null
},

我的 Rails 应用程序的 API 没有父级 (http://www.soleresource.com/releases.json)

[
{
  shoe_name: "Air Jordan 4 "Green Glow"",
  release_date: "2013-08-17T00:00:00.000Z",
  shoe_colorway: "Dark-Grey/Green-Glow",
  shoe_price: "160",
  url: "http://www.soleresource.com/releases/8.json"
},
{
  shoe_name: "Nike Barkley Posite",
  release_date: "2013-08-17T00:00:00.000Z",
  shoe_colorway: "Gamma-Green/Black",
  shoe_price: "235",
  url: "http://www.soleresource.com/releases/17.json"
},

为了让应用程序正常工作,我必须调用父级(帖子),如下所示:

 self.upcomingReleases = [dataDictionary objectForKey:@"posts"];

如何将我的 API “包装”在 Parent 下? (我的模型叫做“Releases”)

【问题讨论】:

    标签: iphone ios ruby-on-rails json api


    【解决方案1】:

    恕我直言,如果您将 Rails 应用程序用作 REST API,则绝对应该使用 ActiveModel::Serializer。它可以让您为 JSON 响应指定所有内容,例如 metadataroots

    这是一个 RailsCast:http://railscasts.com/episodes/409-active-model-serializers

    【讨论】:

    • 谢谢,我会调查的。
    • 或者,在您的 Rails 控制器中执行以下操作:render json: releases: @releases
    最近更新 更多