【发布时间】:2016-01-24 01:02:52
【问题描述】:
将 jsonapi-resources gem 的 0.6.0 版本与 Doorkeeper 结合使用,我无法在资源的 context 对象中看到当前用户。
我基本上是在关注the docs,但是我没有尝试使我在ApplicationController 中设置的context 在资源的fetchable_fields 方法中可见。我确实确认context 实际上是在我的ApplicationController 中设置的。
这就是我所拥有的
应用控制器
class ApplicationController < JSONAPI::ResourceController
protect_from_forgery with: :null_session
def context
{current_user: current_user}
end
end
控制器
class Api::ItemsController < ApplicationController
prepend_before_action :doorkeeper_authorize!
end
资源
class Api::ItemResource < JSONAPI::Resource
# attributes
def fetchable_fields
# context is always nil here
if (context[:current_user].guest)
super - [:field_i_want_private]
else
super
end
end
end
【问题讨论】:
-
你有没有得到答案,我遇到了类似的事情?
-
我没有@withakay。我最终不需要这样做,但我们可能应该在 github 中打开一个问题,因为它似乎不太可能在这里得到解决。
-
我猜
:doorkeeper_authorize!有一个叫current_user的方法?你确定它工作正常吗?
标签: ruby-on-rails json-api jsonapi-resources