Default URL
The default_url
plugin allows setting the URL which will be
returned when there is no attached file.
plugin :default_url
Attacher.default_url do |**options|
"/#{name}/missing.jpg"
end
The Attacher#url
method will return the default URL when attachment is
missing:
user.avatar_url #=> "/avatar/missing.jpg"
# or
attacher.url #=> "/avatar/missing.jpg"
Any URL options passed will be available in the default URL block:
attacher.url(foo: "bar")
Attacher.default_url do |**options|
options #=> { foo: "bar" }
end
The default URL block is evaluated in the context of an instance of
Shrine::Attacher
.
Attacher.default_url do |**options|
self #=> #<Shrine::Attacher>
file #=> #<Shrine::UploadedFile>
name #=> :avatar
record #=> #<User>
context #=> { ... }
# ...
end
Host
If the default URL is relative, the URL host can be specified via the :host
option:
plugin :default_url, host: "https://example.com"
attacher.url #=> "https://example.com/avatar/missing.jpg"