简体   繁体   中英

Rabl and doorkeeper

I develop api for my rails project and don't now how to solve this problem

I use doorkeeper gem for authentication and rabl gem for json rendering. And i have to add some fields only for logged user.

For example: part of show.rabl :

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

And api/application_controller.rb :

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

I also tried this way ( rabl file) :

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

And rabl doesn't know anything about my method and return false in if lambda. How can i solve this problem? i ready for any suggestions. Thank you!

i use rails-3.2.6 doorkeeper-0.4.2 rabl-0.6.13

You can try:

  • setting an instance variable in your controller instead of using current_user
  • or rendering a different file wether the user is logged in or not (and use rabl partial to avoid duplication)

I'm also using rabl and doorkeeper so keep me up to date if you find a good solution ;) I might need to do the same soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM