我可以從控制檯訪問在此API調用中加載的shopping_list_products,沒有錯誤。Class未定義方法'id',但控制檯加載正常
我的API獲得500,但是:
ActionView::Template::Error (undefined method `id' for #<Class:0x007f6cca7e1dd0>):
2015-01-26T23:51:07.608697+00:00 app[web.1]: 1: collection :@shopping_list_products
2015-01-26T23:51:07.608699+00:00 app[web.1]: 2: extends 'api/v1/shopping_list_products/_show'
index.json.rabl:
collection :@shopping_list_products
extends 'api/v1/shopping_list_products/_show'
show.json.rabl:
object :@shopping_list_product
extends 'api/v1/shopping_list_products/_show'
_show.json .rabl:
object :shopping_list_product
attribute(:id, :if => lambda { |m| m.id })
attributes :shopping_list_retailer_id, :product_id, ...
... more attributes
child :product, partial: 'api/v1/products/_show'
child :product_category, partial: 'api/v1/product_categories/_show'
node(:url) do |shopping_list_product|
api_v1_schedule_requisition_plan_shopping_list_shopping_list_retailer_shopping_list_product_path(@schedule, @shopping_list_retailer, shopping_list_product, format: :json)
end
編輯:我刪除了id屬性,然後遇到下一個錯誤,「未定義的方法shopping_list_retailer_id爲類:」。這是爲什麼發生?
編輯:發現這是我的代碼從控制器稱爲..如果我回到
@shopping_list_retailer.shopping_list_products
它工作正常。
但我這樣做,而不是:
api :GET, '/schedule/:schedule_id/requisition_plan/shopping_list/shopping_list_retailers/:shopping_list_retailer_id/shopping_list_products', 'List all shopping list products for Shopping List for Requisition Plans for Schedule in the database'
param :query, String, desc: "Scoped_search style query string"
def index
@shopping_list_products = ShoppingListProductsIndexQuery.new(@shopping_list_retailer, params[:query]).shopping_list_products
end
class ShoppingListProductsIndexQuery
attr_reader :shopping_list_retailer, :query
def initialize(shopping_list_retailer, query)
@shopping_list_retailer = shopping_list_retailer
@query = query
end
def shopping_list_products
@shopping_list_retailer.shopping_list_products.ordered_by_product_category_type_and_product_category_name_and_product_name.search_for(query)
end
end
仍然困惑爲什麼類未定義的方法ID在Rabl的觀點被擊中。
同樣的問題被忽略,仍。 – quantumpotato 2015-01-27 02:18:40