Skip to content

Commit 9e8b6d4

Browse files
committed
Example of model using current_user from context hash
1 parent 2d7e38b commit 9e8b6d4

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
@@ -179,6 +179,14 @@ end
179179
class PeopleController < ApplicationController
180180

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

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

0 commit comments

Comments
 (0)