0

I am trying to output the value of a method on my Item model (current_user is defined in application_controller). I currently have as my rabl template:

object @item
attributes :id, :name

code :is_liked do |this_item|
  if current_user
    this_item.is_liked current_user
  else
    false
  end
end

and in my model:

class Item < ActiveRecord::Base
  ...
  def is_liked user
    if user
      if user.liked_item_ids.include?(self.id)
        return true
      else
        return false
      end
    end
  end
  ....
end

but it isn't working. I'm not sure what a proper way of outputting this would be. Any idea how to get this to work correctly?

edit 1

Here's the error that I'm getting:

 Failure/Error: Unable to find matching line from backtrace
 ActionView::Template::Error:
   stack level too deep
4

2 回答 2

1

您的 rabl 似乎很好,但是,当您发现自己在视图中添加了一些逻辑(可以将 rabl 与视图进行比较)时,您可能需要考虑重构演示者中的逻辑。

有关带有 rabl 的演示者的更多信息,请单击此处

关于您的错误,就像@apneadiving 刚才所说的那样,您的代码库中某处存在递归问题。只是出于好奇,您是否尝试将code块重命名为方法名称以外的其他名称?根据您使用的 rabl 版本,这可能是问题所在。

最后,您应该考虑重构您的is_liked方法:

def is_liked user
  return user.liked_item_ids.include?(id) if user
  false
end
于 2013-07-19T07:42:45.507 回答
0

尝试:

node(:is_liked) {|this_item| this_item.is_liked(current_user) }

您已经有了该方法,您可以在节点内简单地调用这里而不是重新创建逻辑。

于 2013-07-19T07:01:12.307 回答