2015-01-26 2 views
0

오류없이 콘솔에서이 API 호출에로드 된 shopping_list_products에 액세스 할 수 있습니다. 클래스에 대해 정의되지 않은 메소드 '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 

EDIT : id 속성을 제거한 후 다음 오류 인 "Class :에 대해 정의되지 않은 메소드 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보기에 명중 이유.

답변

0

오류 메시지 (undefined method 'id' for #<Class:0x007f6cca7e1dd0>)ShoppingListProduct (또는 실제 클래스 이름이 무엇이든) 대신 Class의 인스턴스에 정의되지 않은 메서드가 있다고 말합니다.

이것은 잘못된 것이 렌더링되고 있음을 의미합니다.

사용하는 아마 때문에

:

object :@shopping_list_products 
# and 
object :@shopping_list_product 

대신 :

object @shopping_list_products 
# and 
object @shopping_list_product 

:의를 제거합니다.

또한, 귀하의 _show.json.rabl 파일에, 당신은 정말 RABL 템플릿이 부분 (https://github.com/nesquena/rabl/wiki/Reusing-templates)

+0

같은 문제로 사용될 때이 같은

object :shopping_list_product 

여전히 무시됩니다 필요가 없습니다. – quantumpotato

관련 문제