Skip to content

Commit 32bfdb0

Browse files
committed
Add url_for example for links
1 parent 97ed37e commit 32bfdb0

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

docs/general/rendering.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ link(:link_name) { "https://example.com/resource/#{object.id}" }
170170

171171
link(:link_name) { resource_url(object) }
172172

173+
link(:link_name) { url_for(controller: 'controller_name', action: 'index', only_path: false) }
174+
173175
```
174176

175177
### serializer_opts

test/adapter/json_api/links_test.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ class LinksTest < ActiveSupport::TestCase
77
LinkAuthor = Class.new(::Model)
88
class LinkAuthorSerializer < ActiveModel::Serializer
99
link :self do
10-
href "//example.com/link_author/#{object.id}"
10+
href "http://example.com/link_author/#{object.id}"
1111
meta stuff: 'value'
1212
end
1313
link(:author) { link_author_url(object.id) }
14-
link(:link_authors) { link_authors_url }
15-
link :other, '//example.com/resource'
14+
link(:link_authors) { url_for(controller: 'link_authors', action: 'index', only_path: false) }
1615
link(:posts) { link_author_posts_url(object.id) }
16+
link :resource, 'http://example.com/resource'
1717
link :yet_another do
18-
"//example.com/resource/#{object.id}"
18+
"http://example.com/resource/#{object.id}"
1919
end
2020
end
2121

@@ -35,15 +35,15 @@ def test_toplevel_links
3535
adapter: :json_api,
3636
links: {
3737
self: {
38-
href: '//example.com/posts',
38+
href: 'http://example.com/posts',
3939
meta: {
4040
stuff: 'value'
4141
}
4242
}
4343
}).serializable_hash
4444
expected = {
4545
self: {
46-
href: '//example.com/posts',
46+
href: 'http://example.com/posts',
4747
meta: {
4848
stuff: 'value'
4949
}
@@ -74,16 +74,16 @@ def test_resource_links
7474
hash = serializable(@author, adapter: :json_api).serializable_hash
7575
expected = {
7676
self: {
77-
href: '//example.com/link_author/1337',
77+
href: 'http://example.com/link_author/1337',
7878
meta: {
7979
stuff: 'value'
8080
}
8181
},
8282
author: 'http://example.com/link_authors/1337',
8383
link_authors: 'http://example.com/link_authors',
84-
other: '//example.com/resource',
84+
resource: 'http://example.com/resource',
8585
posts: 'http://example.com/link_authors/1337/posts',
86-
yet_another: '//example.com/resource/1337'
86+
yet_another: 'http://example.com/resource/1337'
8787
}
8888
assert_equal(expected, hash[:data][:links])
8989
end

0 commit comments

Comments
 (0)