Ruby on Rails send_file, Code in Controller-Aktion, der zweimal ausgeführt wird

Ich habe ein seltsames Problem mit Dateien, die vom Benutzer heruntergeladen werden können. Die Datei wird ordnungsgemäß heruntergeladen, aber der andere Code in der Controller-Aktion wird zweimal ausgeführt. Ich verwende CarrierWave, das unter .file in ein Dokumentmodell eingebunden ist.

Ich möchte, dass Benutzer auf diesen Link klicken können, um die Datei herunterzuladen:

<%= link_to document.file.file.filename, document_path(document)  %>

So sieht mein Controller aus:

  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

Wenn ich auf den Link klicke, wird die Datei heruntergeladen, es werden jedoch 2 CourseDownload-Datensätze erstellt. In den Protokollen sieht es so aus, als ob die GET-Anforderung zweimal ausgeführt wird:

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)

Irgendeine Idee, was ich falsch mache?

Vielen Dank!

Antworten auf die Frage(1)

Ihre Antwort auf die Frage