Rails send files starting multiple GETs - ruby-on-rails

I have 2 Rails apps, App1 and App2.
App1 contains links to controller actions in App2 which in turn returns files (downloads), e.g.:
<%= link_to "Download Video", "http://app2/downloads/download_video", :target => "_bkank" %>
with "downloads" being and controller within App2 and "download_video" being an action of the "downloads" controller:
def download_video
send_file '/path/to/video/video.mp4', type: "video/mp4", :disposition => "inline"
end
Upon clicking the link, a new window (tab) is opened and the video is played in the window - this is a desired behavior .... my problem is, when looking t the logs, I see that the same GET request is sent 3 times - the first as HTML and then as / twice:
Started GET "/downloads/download_video" for ::1 at 2018-04-19 16:26:45 +0200
Processing by DownloadsController#download_video as HTML
Sent file /path/to/video/video.mp4 (0.2ms)
Completed 200 OK in 1ms
Started GET "/downloads/download_video" for ::1 at 2018-04-19 16:26:46 +0200
Processing by DownloadsController#download_video as */*
Sent file /path/to/video/video.mp4 (0.1ms)
Completed 200 OK in 1ms
Started GET "/downloads/download_video" for ::1 at 2018-04-19 16:26:46 +0200
Processing by DownloadsController#download_video as */*
Sent file /path/to/video/video.mp4 (0.1ms)
Completed 200 OK in 0ms
Could anyone let me know why this is happening and how I can prevent it?
Thanks,
Jon.
#praga2050,
can you please post entire controller code ...
yes:
class DownloadsController < ApplicationController
def download_video
send_file '/var/www/videos/video.mp4', type: "video/mp4", :disposition => "inline"
end
end
#praga2050,
... try putting Rails.logger.debug with time stamp and see
yes:
Started GET "/downloads/download_video" for ::1 at 2018-04-20 13:59:42 +0200
Processing by DownloadsController#download_video as HTML
Log Date:2018-04-20T13:59:42+02:00
Sent file /path/to/video/video.mp4 (0.2ms)
Completed 200 OK in 1ms
Started GET "/downloads/download_video" for ::1 at 2018-04-20 13:59:42 +0200
Processing by DownloadsController#download_video as */*
Log Date:2018-04-20T13:59:42+02:00
Sent file /path/to/video/video.mp4 (0.2ms)
Completed 200 OK in 1ms
Started GET "/downloads/download_video" for ::1 at 2018-04-20 13:59:42 +0200
Processing by DownloadsController#download_video as */*
Log Date:2018-04-20T13:59:42+02:00
Sent file /path/to/video/video.mp4 (0.1ms)
Completed 200 OK in 1ms

Related

Rails Receives Repetitive Requests on One Request

My route is as follows:
Rails.application.routes.draw do
get 'courses' => 'application#index'
get 'check_db' => 'application#check_db'
root 'application#index'
end
And my application#check_db is
def check_db
p "test"
redirect_to root_path
end
When I visit /check_db on my browswer (tried both in Chrome and Safari), if working properly, it should GET "/check_db" and then GET "/". But actually, the log shows that there are four requests, with the above pattern repeated twice (i.e. GET "/check_db", GET "/", GET "/check_db", GET "/"). There is absolutely no code in my application#index that does any other redirection. So why is this?
The situation described above happen most of the times. However, occasionally, it works as expected. I did not change the code in the interim.
If it is in interest, the entire log is pasted below:
Started GET "/check_db" for ::1 at 2017-01-04 17:05:06 -0800
Processing by ApplicationController#check_db as HTML
"test"
Redirected to http://localhost:3000/
Completed 302 Found in 1ms
Started GET "/" for ::1 at 2017-01-04 17:05:06 -0800
Processing by ApplicationController#index as HTML
Rendering application/index.html.erb within layouts/application
[MongoDB query log]
Rendered application/index.html.erb within layouts/application (3.4ms)
Completed 200 OK in 18ms (Views: 16.4ms)
Started GET "/check_db" for ::1 at 2017-01-04 17:05:07 -0800
Processing by ApplicationController#check_db as HTML
"test"
Redirected to http://localhost:3000/
Completed 302 Found in 8ms
Started GET "/" for ::1 at 2017-01-04 17:05:07 -0800
Processing by ApplicationController#index as HTML
Rendering application/index.html.erb within layouts/application
[MongoDB query log]
Rendered application/index.html.erb within layouts/application (4.1ms)
Completed 200 OK in 17ms (Views: 15.0ms)

Started DELETE "/users/sign_out" => Processing by Devise::SessionsController#destroy as HTML => User still logged in

When I click 'log out' I can see that Rails processess the request, but I'm still logged in.
Any clue why this happens? Both locally and on Heroku.
Started DELETE "/users/sign_out" for IP at 2014-11-18 08:32:46 +0000
Processing by Devise::SessionsController#destroy as HTML
Parameters: {"authenticity_token"=>"TOKEN"}
Redirected to http://example.com/
Completed 302 Found in 14ms (ActiveRecord: 4.4ms)
Started GET "/" for 84.215.64.133 at 2014-11-18 08:32:47 +0000
Processing by StaticPagesController#blog as HTML
Rendered static_pages/blog.html.slim within layouts/application (0.7ms)
Completed 200 OK in 6ms (Views: 2.9ms | ActiveRecord: 1.6ms)
routes.rb
devise_for :users
From the log when editing a user:
Started PUT "/users" for 84.215.64.133 at 2014-11-18 14:40:30 +0000
Processing by Devise::RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"QR/p3BAG+ocmacss5xDjuFDfhFSA+iv6VRK37uA9HcQ=", "user"=>{"mobile"=>"93441707", "email"=>"sss#
strosin.info", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Lagre"}
2014-11-18T14:40:31.095730+00:00 app[web.2]: App 131 stdout: Redirected to http://
The problem disapeared when I cleared the browser cache. (Using Chrome beta).

send_file not working - Ruby on Rails

I have this action here :
def download
send_file 'C:/Sites/solver/public/test.zip'
end
When I execute it from my html nothing happens and nothing is downloaded.
But here is the log it produce :
Started POST "/ssh/download" for 127.0.0.1 at 2014-04-03 11:06:17 -0400
Processing by SshController#download as */*
Sent file C:/Sites/solver/public/test.zip (0.0ms)
Completed 200 OK in 5ms (ActiveRecord: 0.0ms)
Anyone can help me understand what is missing ?
Thank you!
In your view, add a download option, eg:
link_to 'Download', download_path(id), download: "file"

unable to navigate away from page via browser back button

When these partials are loaded in the browser I am unable to navigate away from the page via the browser back button. The page stays the same but the url changes.
What could be causing this?
match '/signup', to: 'users#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'get'
# /users/new
def new
#user = User.new
render partial: "new", layout: false
end
# /sessions/new
def new
render partial: "new", layout: false
end
Here's my stack. This is from just clicking the signin link once.
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as */*
Processing by SessionsController#new as */*
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 4ms (Views: 3.3ms | ActiveRecord: 0.0ms)
Completed 200 OK in 4ms (Views: 3.3ms | ActiveRecord: 0.0ms)
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as HTML
Processing by SessionsController#new as HTML
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 3ms (Views: 1.9ms | ActiveRecord: 0.0ms)
Completed 200 OK in 3ms (Views: 1.9ms | ActiveRecord: 0.0ms)
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as HTML
Processing by SessionsController#new as HTML
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
Do you have a before filter redirecting public access to sessions#new? Have you defined /signin as a route?
I might be wrong, but as I wrote in the comment while ago I had a customer that used Turbolinks on Rails 3.2.12 and a group of customized JS libraries. The Turbolinks was causing a lot of issues such as double submitting and one of them was a similar thing to what you have mentioned. I had 3 choices:
1- Take out Turbolinks (my temp solution, until I showed the team TL was causing it)
2- Change the JS lib dependencies
3- Update the Rails App to 4.x latest. (I did this finally, it wasn't too bad)
In case if I was wrong and your case and setup was not similar to mine, here is a good thread on Github that some guy tries to solve it and still seems to be open.
https://github.com/rails/turbolinks/issues/256
Check your parameters :
Processing SESSION::SESSION#new (for 127.0.0.1 at 2014-04-04 13:50:03) [POST]
Parameters: {"authenticity_token"=>"XXXXX=", "param1"=>"1"}
Well in your log you will see what params you are sending if it is not sending what you want create params according to your columns or what you want to keep.
If you want create a back button you could try this:
link_to_function "Back", "history.back()"
Check your action form maybe you are sending other url
<% form_tag :controller=>"sessions",:action=>"new" do %>
<% #user.each do |user| %>
<%= user.name %>
<%= user.lastname %>
<% end %>
<% end %>

Is there something wrong with my link_to?

<%= link_to 'show', :controller=>:users, :action=>:show, :id=>u.id, :confirm=>'are you sure?' %>
<%= link_to 'reset pass.', :controller=>:users, :action=>:reset_password, :id=>u.id, :confirm=>'RESET PASSWORD, are you sure?' %>
The first link works fine, but the second is really strange... sometimes it works, and sometimes is doesn't and causes such an error:
LoadError in UsersController#index
Expected
/home/anonym/Documents/RubyOnRails/authorisation/loginout/app/controllers/users_controller.rb
to define UsersController
Moreover it doesn't show confirmation box. Anyway if I remove :confirm option the first error occurs anyway from time to time.
Controller:
#users_controller.rb
class UsersController < ApplicationController
...
def reset_password
#user=User.find_by_id(params[:id])
...
Routes:
#routes.rb
match 'users/:id/reset_password', :to=>"users#reset_password"
It could be understandable to me if it failed everytime but is it's kind of random.
Any ideas why?
UPDATE
This little modification in controller seemed to fix the problem.
#Thread.new{UserMailer.reset_password(#user, passwd).deliver}
UserMailer.reset_password(#user, passwd).deliver
This also caused my sqlite database to collapse (if I clicked the link very fast a few times).
Do you know why this thread might have been such a killer?
And there still is the question why there is no confirmation box?
UPDATE
Started GET "/users/2/reset_password?confirm=RESET+PASSWORD%2C+are+you+sure%3F" for 127.0.0.1 at 2012-02-06 15:03:57 +0100
Processing by UsersController#reset_password as HTML
Parameters: {"confirm"=>"RESET PASSWORD, are you sure?", "id"=>"2"}
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1[0m
[1m[35mUser Load (0.4ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
[1m[36m (0.5ms)[0m [1mSELECT 1 FROM "users" WHERE ("users"."email" = 'my_email#gmail.com' AND "users"."id" != 2) LIMIT 1[0m
[1m[35m (0.3ms)[0m UPDATE "users" SET "last_support_email_sent_time" = '2012-02-06 14:03:58.182567', "encrypted_password" = '5380187f533d14e2867667f8c2c9dc6671b1d930ff892a8cb4da7cdda99d01c8', "updated_at" = '2012-02-06 14:03:58.186194' WHERE "users"."id" = 2
Rendered /home/anonym/Documents/RubyOnRails/authorisation/loginout/app/views/user_mailer/reset_password.html.erb (0.2ms)
Sent mail to my_email#gmail.com (1270ms)
Date: Mon, 06 Feb 2012 15:03:58 +0100
from: User service
To: my_email#gmail.com
Message-ID: <4f2fddce433d3_2c275ca2fa664560#anonym-laptop.mail>
Subject: Your new password
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hello ,</br>
Your new password is: 469ff56728
Redirected to http://localhost:3000/users
Completed 302 Found in 1407ms
Started GET "/users" for 127.0.0.1 at 2012-02-06 15:03:59 +0100
Processing by UsersController#index as HTML
[1m[36mUser Load (0.3ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1[0m
[1m[35mUser Load (1.1ms)[0m SELECT "users".* FROM "users" ORDER BY email
Rendered shared/_add_to_user_index_row.html.erb (0.1ms)
Rendered shared/_add_to_user_index_row.html.erb (0.1ms)
Rendered shared/_add_to_user_index_row.html.erb (0.1ms)
Rendered shared/_add_to_user_index_row.html.erb (0.1ms)
Rendered shared/_add_to_user_index_row.html.erb (0.1ms)
Rendered shared/_add_to_user_index_row.html.erb (0.2ms)
Rendered /home/anonym/Documents/RubyOnRails/authorisation/loginout/app/views/users/index.html.erb within layouts/application (75.8ms)
Completed 200 OK in 197ms (Views: 132.5ms | ActiveRecord: 2.6ms)
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /jquery_ujs.js - 304 Not Modified (0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /application.css - 304 Not Modified (0ms)
Started GET "/assets/loginout.css" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /loginout.css - 404 Not Found (39ms)
ActionController::RoutingError (No route matches [GET] "/assets/loginout.css"):
Rendered /usr/local/rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.1ms)
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /jquery.js - 304 Not Modified (1ms)
Started GET "/assets/messages.js?body=1" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /messages.js - 304 Not Modified (1ms)
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /application.js - 304 Not Modified (2ms)
Started GET "/assets/auth_engine.css?body=1" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /auth_engine.css - 304 Not Modified (2ms)
Started GET "/assets/sessions.css?body=1" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /sessions.css - 304 Not Modified (0ms)
Started GET "/assets/users.css?body=1" for 127.0.0.1 at 2012-02-06 15:04:00 +0100
Served asset /users.css - 304 Not Modified (0ms)
Started GET "/users" for 127.0.0.1 at 2012-02-06 15:04:35 +0100
SQLite3::SQLException: unsupported file format: SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
ActiveRecord::StatementInvalid (SQLite3::SQLException: unsupported file format: SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
):
After that database is broken - server restart doesn't help. This occures from time to time eventhough I no longer use Threads.
Rails is not thread-safe. If you need to perform background operations, use something like Spawn or DelayedJob
As for the confirmation, do you have an application.js in your app/assets/javascript folder, and do you have <%= javascript_include_tag "application" %> in the <head> of your layout?
If the application.js file isn't being loaded, none of the Rails JS will run, which includes confirmations.
Update: I see the problem now. The link_to method takes two types of options, first the options for routing, then the options for the HTML. :confirm is one of the HTML options.
If you're going to pass routing and HTML options, you need to separate the routing options into their own hash first, like so:
<%= link_to 'show', { :controller=>:users, :action=>:show, :id=>u.id }, :confirm=>'are you sure?' %>
<%= link_to 'reset pass.', { :controller=>:users, :action=>:reset_password, :id=>u.id }, :confirm=>'RESET PASSWORD, are you sure?' %>
As the log demonstrates, your confirm is being passed as part of the path generated by the link_to, rather than being included in the HTML of the generated a element.

Resources