【发布时间】:2014-04-22 06:45:12
【问题描述】:
我在用户可下载文件方面遇到了一个奇怪的问题,文件正在正确下载,但控制器操作中的其他代码正在执行两次。我正在使用 CarrierWave,它安装在 .file 的 Document 模型上。
我希望用户能够点击此链接下载文件:
<%= link_to document.file.file.filename, document_path(document) %>
这是我的控制器的样子:
def show
document = Document.find(params[:id])
# Track this download
CourseDownload.create(course: document.course, user: current_user)
# Download the file
send_file 'public' + document.file.url.to_s
end
当我单击链接时,文件下载,但创建了 2 个 CourseDownload 记录。在日志中,GET 请求似乎发生了两次:
Started GET "/documents/1" for 127.0.0.1 at 2014-04-17 18:12:47 -0400
Processing by DocumentsController#show as HTML
Parameters: {"id"=>"1"}
Document Load (0.2ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'xxx' LIMIT 1
CACHE (0.0ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
public/uploads/document/file/1/bootstrap-3.0.0.zip
Sent file public/uploads/document/file/1/bootstrap-3.0.0.zip (0.1ms)
Completed 200 OK in 7ms (ActiveRecord: 0.4ms)
Started GET "/documents/1" for 127.0.0.1 at 2014-04-17 18:12:47 -0400
Processing by DocumentsController#show as HTML
Parameters: {"id"=>"1"}
Document Load (0.2ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'xxx' LIMIT 1
CACHE (0.0ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
public/uploads/document/file/1/bootstrap-3.0.0.zip
Sent file public/uploads/document/file/1/bootstrap-3.0.0.zip (0.1ms)
Completed 200 OK in 5ms (ActiveRecord: 0.3ms)
知道我做错了什么吗?
谢谢!
【问题讨论】:
-
link_to 在哪个视图中?我想知道您是否在 show.html 中有它,这可能意味着您最初加载页面以显示链接,并在单击链接时重新加载页面......所以实际上执行了两次操作。跨度>
标签: ruby-on-rails ruby carrierwave