Closed jahseng-lee closed 2 years ago
I think i've faced a similar issue before: are you using a string or a jsonb column?
Would you able to try using attacher.column_data
/ or attacher.data
according to your particular case?
I think i've faced a similar issue before: are you using a string or a jsonb column?
Would you able to try using
attacher.column_data
/ orattacher.data
according to your particular case?
Same behaviour unfortunately 🙁
FWIW I'm using a string
in the db, not JSONB
I have reproduced the problem here: https://github.com/benkoshy/debugging-shrine-example/tree/photo-test-data-problem
Note: please change the path of the Shrine gemfile to use github, rather than a local repository.
It is perhaps best answered by the maintainers.
Sending a Shrine::UploadedFile
object in request params will cause the string representation of the object to be sent. You want to send a JSON string or a hash instead, which you can do by calling .to_json
or .data
on the uploaded file object.
Sending a
Shrine::UploadedFile
object in request params will cause the string representation of the object to be sent. You want to send a JSON string or a hash instead, which you can do by calling.to_json
or.data
on the uploaded file object.
Thanks! This worked... sorta.
So the key parts are:
Updating my params to return #data
:
let(:valid_params) do
{
display_photo: {
photo: {
image: TestImageData.uploaded_image.data # updated this to use data now
}
}
}
end
Updating uploaded_file
in the Shrine example to use :cache
:
module TestData
# ...
def uploaded_image
# ...
# NOTE: VERY important you use `:cache` here. Shrine 3.0 does NOT support `:storage` option
# see: https://github.com/shrinerb/shrine/blob/master/doc/upgrading_to_3.md#assigning
uploaded_file = Shrine.upload(file, :cache, metadata: false)
Updating photo_params
to use nested attributes:
# NOTE: https://shrinerb.com/docs/getting-started#tab-group-13-content-14
# DOESN'T work. display_photo_params[:photo] seems to return `{}`.
def display_photo_params
params.require(:display_photo).permit(
photo: [
image: [
:id,
:storage,
metadata: [
:filename,
:mime_type,
:size,
]
]
],
)
end
I am wondering now, given my examples above, if a maintainer wants to create more generic versions of said code snippets and update the docs?
Yes, it would definitely be good for the docs to show testing attaching cached files. I will try to get to it at some point, but I would also appreciate a PR 🙂
@janko I won't get around to it today but I'll keep this tab open and see if I have some time in the near future 🙂
Brief Description
I've set up a test case to try to test my controller, which uploads a (nested) photo within the params. I use the given example in the docs and everything, which works for manually creating an upload in test but does not for sending it over a request.
Expected behavior
I would expect
post some_path, params: { photo: { image: TestData.uploaded_image } }
to work correctlyActual behavior
So it looks like the
post
request sends though an "Uploaded File" as a string, as opposed to an object shrine can interpret.Simplest self-contained example code to demonstrate issue
In my code:
In my request spec:
System configuration
Ruby version:
ruby 2.7.2p137
Rails 6.0.3.6
Shrine version:
gem 'shrine', '~> 3.0'