2015-01-26 70 views
0

我可以从控制台访问在此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的观点被击中。

回答

0

您的错误信息(undefined method 'id' for #<Class:0x007f6cca7e1dd0>)是说未定义的方法是在Class的实例上,而不是ShoppingListProduct(或任何您的实际类名称)的实例。

这意味着正在呈现错误的东西。

可能是因为你使用:

object :@shopping_list_products 
# and 
object :@shopping_list_product 

相反的:

object @shopping_list_products 
# and 
object @shopping_list_product 

取出:秒。

而且,在你的_show.json.rabl文件,你真的不需要

object :shopping_list_product 

,因为这时候Rabl的模板被用作局部(https://github.com/nesquena/rabl/wiki/Reusing-templates

+0

同样的问题被忽略,仍。 – quantumpotato 2015-01-27 02:18:40

相关问题