【发布时间】:2014-04-19 17:49:35
【问题描述】:
如何使用葡萄实体将参数传递给模型方法?
我想在展示物品的时候检查一下current_user是否喜欢一个物品,所以我建立了一个模型user_likes?方法:
class Item
include Mongoid::Document
#some attributes here...
has_and_belongs_to_many :likers
def user_likes?(user)
likers.include?(user)
end
end
但我不知道如何将 current_user 发送到葡萄实体模型:
module FancyApp
module Entities
class Item < Grape::Entity
expose :name #easy
expose :user_likes # <= How can I send an argument to this guy ?
end
end
end
在葡萄 api 中:
get :id do
item = Item.find(.....)
present item, with: FancyApp::Entities::Item # I should probably send current_user here, but how ?
end
我觉得可能应该从最后一段代码发送 current_user,但我不知道该怎么做:(
有什么想法吗? 谢谢!
【问题讨论】:
标签: ruby-on-rails devise grape grape-api