Ruby on rails 类的未定义方法“id”,但控制台可以正常加载

Ruby on rails 类的未定义方法“id”,但控制台可以正常加载,ruby-on-rails,ruby,rabl,Ruby On Rails,Ruby,Rabl,我可以从控制台无误地访问此API调用中加载的购物列表产品 我在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

我可以从控制台无误地访问此API调用中加载的购物列表产品

我在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'
show.json.rabl:

collection :@shopping_list_products
extends 'api/v1/shopping_list_products/_show'
object :@shopping_list_product
extends 'api/v1/shopping_list_products/_show'
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
_show.json.rabl:

collection :@shopping_list_products
extends 'api/v1/shopping_list_products/_show'
object :@shopping_list_product
extends 'api/v1/shopping_list_products/_show'
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\u list\u retailer\u id for Class:。为什么会这样

编辑:发现这是我从控制器调用的代码。。如果我回来

@购物清单零售商。购物清单产品 它很好用

但我会这样做:

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视图中被命中。

的错误消息未定义方法“id”表示未定义的方法位于类的实例上,而不是ShoppingListProduct的实例或任何实际的类名

这意味着渲染的对象是错误的

可能是因为您正在使用:

object :@shopping_list_products
# and
object :@shopping_list_product
而不是:

object @shopping_list_products
# and
object @shopping_list_product
移除:s

另外,在_show.json.rabl文件中,您确实不需要

object :shopping_list_product
因为当RABL模板用作部分模板时,会忽略这一点