Skip to content

Commit e9bcc4e

Browse files
committed
Merge pull request #659 from danielpclark/patch-1
Documentation of model using current_user from context hash
2 parents 4f37205 + 9e8b6d4 commit e9bcc4e

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ end
177177
class PeopleController < ApplicationController
178178

179179
end
180+
181+
# Assuming you don't permit user_id (so the client won't assign a wrong user to own the object)
182+
# you can ensure the current user is assigned the record by using the controller's context hash.
183+
class PeopleResource < JSONAPI::Resource
184+
before_save do
185+
@model.user_id = context[:current_user].id if @model.new_record?
186+
end
187+
end
180188
```
181189

182190
You can put things that affect serialization and resource configuration into the context.

0 commit comments

Comments
 (0)