mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Provide Rails url_helpers via SerializationContext
This commit is contained in:
@@ -96,3 +96,12 @@ class PostsController < ApplicationController
|
||||
|
||||
end
|
||||
```
|
||||
|
||||
If you wish to use Rails url helpers for link generation, e.g., `link(:resources) { resources_url }`, ensure your application sets
|
||||
`Rails.application.routes.default_url_options`.
|
||||
|
||||
```ruby
|
||||
Rails.application.routes.default_url_options = {
|
||||
host: 'example.com'
|
||||
}
|
||||
```
|
||||
|
||||
@@ -103,7 +103,10 @@ PR please :)
|
||||
|
||||
#### links
|
||||
|
||||
##### How to add top-level links
|
||||
If you wish to use Rails url helpers for link generation, e.g., `link(:resources) { resources_url }`, ensure your application sets
|
||||
`Rails.application.routes.default_url_options`.
|
||||
|
||||
##### Top-level
|
||||
|
||||
JsonApi supports a [links object](http://jsonapi.org/format/#document-links) to be specified at top-level, that you can specify in the `render`:
|
||||
|
||||
@@ -144,6 +147,33 @@ That's the result:
|
||||
|
||||
This feature is specific to JsonApi, so you have to use the use the [JsonApi Adapter](adapters.md#jsonapi)
|
||||
|
||||
|
||||
##### Resource-level
|
||||
|
||||
In your serializer, define each link in one of the following methods:
|
||||
|
||||
As a static string
|
||||
|
||||
```ruby
|
||||
link :link_name, 'https://example.com/resource'
|
||||
```
|
||||
|
||||
As a block to be evaluated. When using Rails, URL helpers are available.
|
||||
Ensure your application sets `Rails.application.routes.default_url_options`.
|
||||
|
||||
```ruby
|
||||
link :link_name_ do
|
||||
"https://example.com/resource/#{object.id}"
|
||||
end
|
||||
|
||||
link(:link_name) { "https://example.com/resource/#{object.id}" }
|
||||
|
||||
link(:link_name) { resource_url(object) }
|
||||
|
||||
link(:link_name) { url_for(controller: 'controller_name', action: 'index', only_path: false) }
|
||||
|
||||
```
|
||||
|
||||
### serializer_opts
|
||||
|
||||
#### include
|
||||
|
||||
@@ -135,13 +135,15 @@ With the `:json_api` adapter, the previous serializers would be rendered as:
|
||||
|
||||
#### ::link
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
link :other, 'https://example.com/resource'
|
||||
link :self do
|
||||
href "https://example.com/link_author/#{object.id}"
|
||||
href "https://example.com/link_author/#{object.id}"
|
||||
end
|
||||
link :author { link_author_url(object) }
|
||||
link :link_authors { link_authors_url }
|
||||
link :other, 'https://example.com/resource'
|
||||
link :posts { link_author_posts_url(object) }
|
||||
```
|
||||
```
|
||||
|
||||
#### #object
|
||||
|
||||
Reference in New Issue
Block a user