I wanted to save the HTML including the image URL as it is in the DB and display it in raw with an editor such as WYSIWY.
If you save the service_url
directly, the access token will expire and you will get the following error:
https://storage.googleapis.com/xxx/6qk30zadasnfkansfalksj34gq?GoogleAccessId=...
<Error>
<Code>ExpiredToken</Code>
<Message>The provided token has expired.</Message>
<Details>Request signature expired at: 2020-09-30T19:07:20+00:00</Details>
</Error>
Create a model for assets to prepare a URL to generate and display a service_url with an access token.
asset.rb
class Asset < ApplicationRecord
has_one_attached :image
end
Changed to send image data with show of controller.
assets_controller.rb
class AssetsController < ActionController::Base
# GET /assets:id
def show
asset = Asset.find(params[:id])
send_data asset.image.download, filename: asset.image.filename.to_s, content_type: asset.image.content_type
end
end
By saving the following URL in the DB, it was possible to display the image via the application server without expiring.
...
<img src="http://localhost:3000/assets/2" alt="Image">
...
Recommended Posts