gpt4 book ai didi

ruby-on-rails - 在 Rails 中,我们不能一起使用 send_file 和 redirect_to 吗?多次调用渲染和/或重定向错误

转载 作者:行者123 更新时间:2023-12-04 07:28:31 25 4
gpt4 key购买 nike

这是我的行动:

def download
@photo = Photo.friendly.find(params[:photo_id])
if @photo.free?
size = Size.find(params[:size])

img = Magick::Image.read(@photo.file.file.file).first
img.resize!(size.width, size.height)

path = "#{Rails.root}/tempfreedownload/#{size.width}x#{size.height}-#{@photo.file.file.filename}"
File.write(path, '')
img.write path

url1_data = open(path)
send_file url1_data

File.delete path
img.destroy!

downloads = @photo.downloads + 1
@photo.update_attribute(:downloads, downloads)

flash[:success] = 'Thanks for downloading this image!'
redirect_to(@photo)
return
else
render file: "#{Rails.root}/public/404.html", layout: false, status: 404
end
end

我想要做的是发送图像进行下载,然后重定向到照片网址。问题是,在这里我得到一个 Render and/or redirect were called multiple times错误。我怎样才能解决这个问题?

最佳答案

您正在尝试发送两个响应。一个是文件数据,一个是重定向。 Rails 不允许您在 Controller 中执行此操作。它可以用 javascript 来实现,但它有点 hacky。看到这个:Rails how do I - export data with send_data then redirect_to a new page?

关于ruby-on-rails - 在 Rails 中,我们不能一起使用 send_file 和 redirect_to 吗?多次调用渲染和/或重定向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30296653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com