ActiveResource ArgumentError(期望的属性能够转换为Hash

时间:2018-05-11 16:54:50

标签: ruby-on-rails activeresource

当ActiveResource尝试解码来自API的响应时,我收到此错误:

  

ArgumentError(期望属性能够转换为Hash,得到[“menu_items”,...

重要的宝石版本:

  • Rails:5.1.6
  • ActiveResource:5.0.0

这是我的API调用模型:

class Menu < ActiveResource::Base
  self.site = "https://blah.asd.com"
  self.prefix = "/api/"
  self.element_name = "menu"

  has_many :menu_items
  has_many :ad_units

  def self.collection_name
    element_name
  end

end

以下是儿童模特:

class AdUnit < ActiveResource::Base
  self.site = "https://blah.asd.com"
  self.prefix = "/api/"
  self.element_name = "ad_unit"

  belongs_to :menu

end
class MenuItem < ActiveResource::Base
  self.site = "https://blah.asd.com"
  self.prefix = "/api/"
  self.element_name = "menu_item"

  belongs_to :menu

end

最后,这是来自api的json:

{
      "menu_items":[
         {
            "id":17,
            "name":"Turkey Bacon Avocado Sandwich",
            "price":5.99,
            "original_price":9.99,
            "description":"Roast turkey, bacon, swiss, lettuce, tomato, avocado &amp; pesto mayo on toasted whole wheat.",
            "restaurant_id":25
         },
         {
            "id":13,
            "name":"Wild Salmon",
            "price":9.99,
            "original_price":13.99,
            "description":"Wild fresh herbed seasoned salmon prepared daily with tzatziki sauce and served with fresh beets &amp; onion and roasted veggies.",
            "restaurant_id":25
         },
         {
            "id":16,
            "name":"BLT+",
            "price":9.99,
            "original_price":9.99,
            "description":"Loaded with crispy bacon, leaf lettuce, sliced tomato, provolone &amp; avocado on panini.",
            "restaurant_id":25
         },
         {
            "id":9,
            "name":"Indian Chicken Curry",
            "price":11.99,
            "original_price":11.99,
            "description":"Chicken cooked in a mild sauce.",
            "restaurant_id":24
         },
         {
            "id":10,
            "name":"Indian Goat Curry",
            "price":12.99,
            "original_price":12.99,
            "description":"Goat cooked in a mild sauce.",
            "restaurant_id":24
         },
         {
            "id":11,
            "name":"Indian Curry Combo",
            "price":12.99,
            "original_price":12.99,
            "description":"Combination of Goat and Chicken Curry, cooked in a mild sauce. Served with rice &amp; naan",
            "restaurant_id":24
         },
         {
            "id":12,
            "name":"Indian Goat &amp; Chicken Combo",
            "price":12.99,
            "original_price":12.99,
            "description":"Combination of Goat Curry &amp; Butter Chicken. Served with rice and naan.",
            "restaurant_id":24
         }
      ],
      "ad_units":[
         {
            "id":3,
            "ad_unit_id":2,
            "position":2,
            "name":"This is a great ad",
            "description":"This is a description of an ad unit"
         }
      ]
}

我希望将此响应转换为适当的ActiveResource对象,但由于某种原因,它会抛出异常。

更新#1:

我已经使用gem repositoy中的代码覆盖了我的Menu模型中的load(attributes,remove_root = false,persisted = false)方法。这是属性对象的样子:

menu_items
{"id"=>17, "name"=>"Turkey Bacon Avocado Sandwich", "price"=>5.99, "original_price"=>9.99, "description"=>"Roast turkey, bacon, swiss, lettuce, tomato, avocado &amp; pesto mayo on toasted whole wheat.", "restaurant_id"=>25}
{"id"=>13, "name"=>"Wild Salmon", "price"=>9.99, "original_price"=>13.99, "description"=>"Wild fresh herbed seasoned salmon prepared daily with tzatziki sauce and served with fresh beets &amp; onion and roasted veggies.", "restaurant_id"=>25}
{"id"=>16, "name"=>"BLT+", "price"=>9.99, "original_price"=>9.99, "description"=>"Loaded with crispy bacon, leaf lettuce, sliced tomato, provolone &amp; avocado on panini.", "restaurant_id"=>25}
{"id"=>9, "name"=>"Indian Chicken Curry", "price"=>11.99, "original_price"=>11.99, "description"=>"Chicken cooked in a mild sauce.", "restaurant_id"=>24}
{"id"=>10, "name"=>"Indian Goat Curry", "price"=>12.99, "original_price"=>12.99, "description"=>"Goat cooked in a mild sauce.", "restaurant_id"=>24}
{"id"=>11, "name"=>"Indian Curry Combo", "price"=>12.99, "original_price"=>12.99, "description"=>"Combination of Goat and Chicken Curry, cooked in a mild sauce. Served with rice &amp; naan", "restaurant_id"=>24}
{"id"=>12, "name"=>"Indian Goat &amp; Chicken Combo", "price"=>12.99, "original_price"=>12.99, "description"=>"Combination of Goat Curry &amp; Butter Chicken. Served with rice and naan.", "restaurant_id"=>24}

更新#2:

无论我发送什么,似乎activeresource似乎无法解码它。即使{“无”:“无”}也会出现同样的错误。

  

ArgumentError(期望属性能够转换为Hash,得到[“没有”,“没有”]):

也许我错过了一些格式库?我从项目中删除了ActiveRecords,要求我从application.rb文件中删除require'trail / all'。

我已将'rails / all'替换为:

require "active_model/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"

0 个答案:

没有答案