英文:
Show image from controller in rails
问题
I am displaying an image in view using
<%= image_tag(@staff.attachment_url(@staff.image), alt: "No image", width: "100px", height: "100px") %>
which is working fine.
Here attachment_url
is a method for creating a dynamic URL every time you open the image, i.e., staff_profiles/images/b64f8b457dddf968712401bf07a8d08ba7f1ce3a326598ec2144f1c995309e06
, and also this image is valid for 30 seconds only. After 30 seconds, when you refresh or open the same URL, it will show Access denied
.
Everything here is working fine.
Like this image, I have other attachments to download/view with a download link. When any user opens the page, this attachment_url
is called at the time of page load/show method. But after 30 seconds, this download link expires, and when the user clicks on the link, it shows Access Denied
. I want to call the attachment_url
when the user clicks on the download link after 1 minute or 5 minutes or 10 minutes of page load.
So I need to open the image or create a download link URL from the controller, not like mentioned above, and also want to use the method given above attachment_url
, which will help to create a dynamic URL.
Thanks
英文:
I am displaying an image in view using
<%= image_tag(@staff.attachment_url(@staff.image), alt: "No image", width: "100px", height: "100px") %>
which is working fine.
Here attachment_url
is method for creating dynamic url every time you open the image
i.e. staff_profiles/images/b64f8b457dddf968712401bf07a8d08ba7f1ce3a326598ec2144f1c995309e06
and also this image is valid for 30 second only and after 30 second when you refresh or open the same url it will show Access denied
.
Everything here is working fine.
Like this image i have other attachments to download/view with download link. when any user open page this attachment_url calls at the time of page load / show method but after 30 seconds this download link expires and when user click on the link it shows Access Denied
. I want to call the attachment_url
when user click on download link after 1 minute or 5 minutes or 10 minutes of page load.
So I need to open the image or create download link url from controller, Not like mentioned above and also want to use the method given above attachment_url
which will help to create url dynamic.
Thanks
答案1
得分: 0
要显示来自控制器操作的图像,您可以使用send_file。
# 在您的控制器中
def attachment_link
# 构建动态URL的逻辑
image_url = @staff.attachment_url(@staff.image)
# 类型:添加正确的文件内容类型
# disposition:'inline'将在浏览器中显示文件
send_file image_url, type: "image/png", disposition: "inline"
end
英文:
To display an image from a controller's action. You can use send_file.
# in your controller
def attachment_link
# logic to build dynamic URL
image_url = @staff.attachment_url(@staff.image)
# type: add the correct file content type
# disposition: 'inline' will display file in browser
send_file image_url, type: "image/png", disposition: "inline"
end
答案2
得分: 0
def attachment_link
# 生成动态URL的逻辑
image_url = @staff.attachment_url(@staff.image)
redirect_to image_url
end
在上面的控制器中对我有用
英文:
def attachment_link
# logic to build dynamic URL
image_url = @staff.attachment_url(@staff.image)
redirect_to image_url
end
In Controller above works for me
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论