Ruby on rails 使用返回嵌套哈希的Grape创建api端点

Ruby on rails 使用返回嵌套哈希的Grape创建api端点,ruby-on-rails,ruby,json,ruby-on-rails-4,grape,Ruby On Rails,Ruby,Json,Ruby On Rails 4,Grape,我在尝试设置第一个API时遇到问题。我的API端点与Grape一起工作,但我不确定如何获得所需的正确数据散列 目前,API非常简单,如下所示: module CategoryTopic class API < Grape::API prefix "api/v1" format :json resource "categories" do get do Category.all

我在尝试设置第一个API时遇到问题。我的API端点与Grape一起工作,但我不确定如何获得所需的正确数据散列

目前,API非常简单,如下所示:

    module CategoryTopic
      class API < Grape::API
        prefix "api/v1"
        format :json

        resource "categories" do
          get do
            Category.all
          end
        end

      end
    end
{categories: 
  {"Numbers": "1", "2"},
  {"Colors": "Red", "Blue"}
}
[
    { "id":"1", "category_name": "General Questions", 
          topics: [ { "numbers":["1", "2"], "colors": ["Red", "Blue"] } ] }
]
require 'grape-entity'

module CategoryTopic
  class API < Grape::API
    prefix "api/v1"
    format :json

    resource "categories" do
      get do
        present Category.all, :with => CategoryEntity
      end
    end

  end
end
但在我的应用程序中,一个类别有很多主题,我想返回一个如下所示的哈希:

    module CategoryTopic
      class API < Grape::API
        prefix "api/v1"
        format :json

        resource "categories" do
          get do
            Category.all
          end
        end

      end
    end
{categories: 
  {"Numbers": "1", "2"},
  {"Colors": "Red", "Blue"}
}
[
    { "id":"1", "category_name": "General Questions", 
          topics: [ { "numbers":["1", "2"], "colors": ["Red", "Blue"] } ] }
]
require 'grape-entity'

module CategoryTopic
  class API < Grape::API
    prefix "api/v1"
    format :json

    resource "categories" do
      get do
        present Category.all, :with => CategoryEntity
      end
    end

  end
end

这样的事情可能吗?

是的,可能。我假设您希望从/api/v1/categoriesURL返回JSON

首先,我不同意您提出的JSON,因为它似乎不正确。如果一个类别与主题有很多关联,那么结果JSON应该返回类别属性及其内部的所有相关关联。在我看来,应该是这样的:

    module CategoryTopic
      class API < Grape::API
        prefix "api/v1"
        format :json

        resource "categories" do
          get do
            Category.all
          end
        end

      end
    end
{categories: 
  {"Numbers": "1", "2"},
  {"Colors": "Red", "Blue"}
}
[
    { "id":"1", "category_name": "General Questions", 
          topics: [ { "numbers":["1", "2"], "colors": ["Red", "Blue"] } ] }
]
require 'grape-entity'

module CategoryTopic
  class API < Grape::API
    prefix "api/v1"
    format :json

    resource "categories" do
      get do
        present Category.all, :with => CategoryEntity
      end
    end

  end
end
在这种情况下,您必须安装Grape实体gem()并创建两个如下实体:

class CategoryEntity < Grape::Entity
    expose :id
    expose :category_name
    expose :topics, :using => TopicEntity
end

class TopicEntity < Grape::Entity
    expose :numbers
    expose :colors
end
类类别实体TopicEntity
结束
类TopicEntity
最好不要直接从API返回模型。您应该使用一个实体来屏蔽API客户机对您的模型表示。接下来,需要此gem,并在API类中使用全新的实体,如下所示:

    module CategoryTopic
      class API < Grape::API
        prefix "api/v1"
        format :json

        resource "categories" do
          get do
            Category.all
          end
        end

      end
    end
{categories: 
  {"Numbers": "1", "2"},
  {"Colors": "Red", "Blue"}
}
[
    { "id":"1", "category_name": "General Questions", 
          topics: [ { "numbers":["1", "2"], "colors": ["Red", "Blue"] } ] }
]
require 'grape-entity'

module CategoryTopic
  class API < Grape::API
    prefix "api/v1"
    format :json

    resource "categories" do
      get do
        present Category.all, :with => CategoryEntity
      end
    end

  end
end
需要“葡萄实体”
模块类别
类APICategoryEntity
结束
结束
结束
结束