Skip to content

Commit 33dc8dd

Browse files
authored
Merge pull request #821 from kevinzwh/serializer-context-note
Add note clarifying context hash usage when passing resources through the ResourceSerializer
2 parents 7820504 + e641600 commit 33dc8dd

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,15 @@ post = Post.find(1)
14951495
JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(PostResource.new(post, nil))
14961496
```
14971497

1498+
Note: If your resource needs to access to state from a context hash, make sure to pass the context hash as the second argument of
1499+
the resource class new method. For example:
1500+
1501+
```ruby
1502+
post = Post.find(1)
1503+
context = { current_user: current_user }
1504+
JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(PostResource.new(post, context))
1505+
```
1506+
14981507
This returns results like this:
14991508

15001509
```json

0 commit comments

Comments
 (0)