2012-08-13 36 views
0

我開發我的Rails項目API和現在不如何解決這個問題Rabl的和看門

我用doorkeeper寶石進行身份驗證和rabl寶石的JSON渲染。我必須爲登錄用戶添加一些字段只有

比如:show.rabl部分:

node :accessible, if: lambda { |m| current_user } do |model| 
    model.accessible(current_user) 
end 

而且api/application_controller.rb

def current_user 
    User.find(doorkeper_token.resource_owner_id) if doorkeper_token 
end 

我也試過這種方式(rabl文件):

if current_user 
    node(:accessible) { |m| m.accessible(current_user) } 
else 
    {no: "no"} # this line rendered!! 
end 

而且Rabl的不瞭解我的方法,並在返回falselambda。 我該如何解決這個問題?我準備好任何建議。謝謝!

我用rails-3.2.6 doorkeeper-0.4.2 rabl-0.6.13

回答

1

你可以試試:

  • 在控制器中設置一個實例變量,而不是使用current_user
  • 或呈現不同的文件閹用戶登錄或沒有(使用拉布爾部分以避免重複)

我也使用拉布爾和門衛所以讓我保持最新,如果你f提供一個很好的解決方案;)我可能需要儘快做到這一點。

+0

謝謝你的回答。但我現在不需要幫助。這是我的錯誤;)我忘記了'access_token'在我的params中,'doorkeper_token'沒有初始化,這就是爲什麼沒有'current_user'。 但我可以補充說,這種方法工作得很好;) – caulfield 2012-08-16 07:44:36

+0

好吧,一切都很好;) – Robin 2012-08-16 12:48:21