Я получаю эту ошибку, когда 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
И, наконец, вот json из API:
{
"menu_items":[
{
"id":17,
"name":"Turkey Bacon Avocado Sandwich",
"price":5.99,
"original_price":9.99,
"description":"Roast turkey, bacon, swiss, lettuce, tomato, avocado & 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 & 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 & 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 & naan",
"restaurant_id":24
},
{
"id":12,
"name":"Indian Goat & Chicken Combo",
"price":12.99,
"original_price":12.99,
"description":"Combination of Goat Curry & 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"
}
]
}
I 'я ожидал, что этот ответ будет преобразован в соответствующие объекты ActiveResource, но по какой-то причине он вызывает исключение.
Обновление № 1:
Я переопределил загрузку (attribute, remove_root = false, persisted = false) в моей модели Menu с кодом в репозитории gem. Вот как выглядит объект атрибутов:
menu_items
{"id"=>17, "name"=>"Turkey Bacon Avocado Sandwich", "price"=>5.99, "original_price"=>9.99, "description"=>"Roast turkey, bacon, swiss, lettuce, tomato, avocado & 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 & 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 & 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 & naan", "restaurant_id"=>24}
{"id"=>12, "name"=>"Indian Goat & Chicken Combo", "price"=>12.99, "original_price"=>12.99, "description"=>"Combination of Goat Curry & Butter Chicken. Served with rice and naan.", "restaurant_id"=>24}
Update # 2:
Появляется независимо от того, чтоЯ посылаю, activeresource не может декодировать его. Даже {"nothing": "nothing"} получаетта же ошибка.
ArgumentError (ожидаемые атрибуты, которые можно преобразовать в Hash, получил ["ничто", "ничто"]):
Может быть, яотсутствует какая-то библиотека форматирования?Я удалил ActiveRecords из своего проекта, который потребовал от меня удалить require 'rails / all' из моего файла application.rb.
Я заменил require '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"