Reputation: 19929
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
Upvotes: 1
Views: 417
Reputation: 115511
Try:
node(:is_liked) {|this_item| this_item.is_liked(current_user) }
You already have the method, you can simply invoke here within a node instead of recreating logic.
Upvotes: 1
Reputation: 17631
Your rabl seems to be fine, however, when your find yourself adding some logic in your views (rabl can be compared to a view) you might want to consider refactor the logic in a presenter.
More information about presenters with rabl here
Regarding your error, like @apneadiving just said, there is a recursion issue in your codebase somewhere. Just by curiosity, have you try to rename the code
block into something else than your method's name ? Depending on which version on rabl you are using, this could be the issue.
Finaly, you should consider refactoring your is_liked
method:
def is_liked user
return user.liked_item_ids.include?(id) if user
false
end
Upvotes: 1