代码之家  ›  专栏  ›  技术社区  ›  moondaisy

FlexRest处理字符串数组

  •  0
  • moondaisy  · 技术社区  · 8 年前

    我使用FlexRest向一个端点发出请求,该端点返回一个字符串数组 Content-Type: application\json .

    这就是模型:

    class Neighborhood < BaseModel
      get :by_location, "/neighborhoods/by_location"
    end 
    

    我就是这样提出请求的:

    def get_neighborhood_list
      city_to_use = ApplicationHelper.user_location[:city] || Rails.configuration.default_location[:city]
      @hoods = Neighborhood.by_location(city: city_to_use)
    end
    

    这被称为,但它只到达 super 因为它在里面断裂了:

    def handle_response(response, cached = nil)
      result = super(response, cached)
      setup_paging_variables result, response
      setup_paging_variables result.data, response if result.respond_to? 'data'
      result
    end
    

    端点返回一个字符串数组:

    [
        "Ahwatukee Foothills Village",
        "Alhambra",
        "Camelback East Village",
        "Central City",
    ]
    

    用这个标题

    enter image description here

    我的问题是,FlexRest将字符串视为json对象,这是因为 Content-Type 头球。 我得到了这个错误

    TypeError:没有将符号隐式转换为整数

    在这条线上 if attributes[:_links] . 它到达那里是因为 hal_response? 这是真的

    为了让FlexRest停止这样的行为,我需要特别做些什么吗?


    使现代化

    我想我没解释清楚。错误发生在gem端。 以下是全部错误:

    TypeError - no implicit conversion of Symbol into Integer:
      flexirest (1.5.5) lib/flexirest/request.rb:612:in `handle_hal_links_embedded'
      flexirest (1.5.5) lib/flexirest/request.rb:553:in `new_object'
      flexirest (1.5.5) lib/flexirest/request.rb:704:in `block in generate_new_object'
      flexirest (1.5.5) lib/flexirest/request.rb:703:in `generate_new_object'
      flexirest (1.5.5) lib/flexirest/request.rb:501:in `handle_response'
      config/initializers/01_flexirest.rb:7:in `handle_response'
      flexirest (1.5.5) lib/flexirest/request.rb:219:in `block (2 levels) in call'
      faraday (0.9.2) lib/faraday/response.rb:57:in `on_complete'
      flexirest (1.5.5) lib/flexirest/request.rb:200:in `block in call'
      activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
      activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
      activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
      flexirest (1.5.5) lib/flexirest/request.rb:156:in `call'
      flexirest (1.5.5) lib/flexirest/mapping.rb:46:in `_call'
      flexirest (1.5.5) lib/flexirest/mapping.rb:28:in `block in _map_call'
      app/controllers/application_controller.rb:93:in `get_neighborhood_list
    

    这个 如果属性[:_链接] 行是gem代码的一部分,它是 here on this line

    当它到达这一行时 attributes 是响应中的字符串之一,例如: attributes = "Ahwatukee Foothills Village" .这就是它给出错误的原因。

    我想弄清楚的是,是否有一种方法(可能是配置中的某种东西)可以使用FlexRest来处理此类响应。

    0 回复  |  直到 8 年前
        1
  •  0
  •   Devon Quick    6 年前

    这是个老问题,但我想我会回答的。你需要补充

    朴素:没错

    在你的模型中,如下所示。

    class Neighborhood < BaseModel
      get :by_location, "/neighborhoods/by_location", plain: true
    end 
    

    然后它将返回字符串数组