#html #ruby-on-rails #ruby #ruby-on-rails-5 #grover
#HTML #ruby-on-rails #ruby #ruby-on-rails-5 #гровер
Вопрос:
Я столкнулся с проблемой, когда загружается только последний файл pdf, хотя я нажимаю на несколько записей для параллельной загрузки.
Вид
<%= link_to(inline_mat_svg :download, stock_item_generate_part_tag_path(stock_item, format: :pdf) %>
Контроллер:
respond_to do |format|
format.html
format.pdf do
pdf = Grover.new("#{request.original_url[0..-5]}?amp;token=#{@report_user.token}",
{format: 'A4',
margin: {
top: '5px',
bottom: '5px',
left: '10px',
right: '10px'
},
viewport: {
width: 1280,
height: 1024
},
prefer_css_page_size: true,
emulate_media: 'screen',
# cache: false,
timeout: 0, # Timeout in ms. A value of `0` means 'no timeout'
launch_args: ['--font-render-hinting=medium'],
executable_path: Rails.configuration.chromium_executable_path,
wait_until: 'networkidle0'
}).to_pdf
send_data pdf, type: 'application/pdf', filename: "Test_#{Time.now.strftime('%m-%d-%Y')}.pdf", disposition: :attachment
Когда пользователь нажимает на несколько записей параллельно, загружается только последний PDF-файл. Но в консоли отображаются несколько файлов PDF.
Консоль:
Started GET "/stock_items/502393374/part_tag.pdf" for ::1 at 2020-11-09 19:25:10 -0500
Processing by StockItemsController#part_tag as PDF
Parameters: {"stock_item_id"=>"502393374"}
User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1
↳ app/controllers/concerns/authentication.rb:58:in `in_development_mode?'
Profile Load (0.7ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 185443385 LIMIT 1
↳ app/models/ability.rb:10:in `block in initialize'
Permission Load (1.1ms) SELECT DISTINCT `permissions`.* FROM `permissions` INNER JOIN `permissions_profiles` ON `permissions`.`id` = `permissions_profiles`.`permission_id` WHERE `permissions_profiles`.`profile_id` = 185443385
↳ app/models/ability.rb:10:in `block in initialize'
StockItem Load (0.8ms) SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 502393374 LIMIT 1
↳ app/controllers/stock_items_controller.rb:104:in `part_tag'
User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1
↳ app/controllers/stock_items_controller.rb:105:in `part_tag'
Started GET "/stock_items/747718251/part_tag.pdf" for ::1 at 2020-11-09 19:25:11 -0500
Processing by StockItemsController#part_tag as PDF
Parameters: {"stock_item_id"=>"747718251"}
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'abc.com' LIMIT 1
↳ app/controllers/concerns/authentication.rb:58:in `in_development_mode?'
Profile Load (0.7ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 185443385 LIMIT 1
↳ app/models/ability.rb:10:in `block in initialize'
Permission Load (0.8ms) SELECT DISTINCT `permissions`.* FROM `permissions` INNER JOIN `permissions_profiles` ON `permissions`.`id` = `permissions_profiles`.`permission_id` WHERE `permissions_profiles`.`profile_id` = 185443385
↳ app/models/ability.rb:10:in `block in initialize'
StockItem Load (0.8ms) SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 747718251 LIMIT 1
↳ app/controllers/stock_items_controller.rb:104:in `part_tag'
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1
↳ app/controllers/stock_items_controller.rb:105:in `part_tag'
Started GET "/stock_items/502393374/part_tag?amp;token=ccc" for ::1 at 2020-11-09 19:25:12 -0500
Processing by StockItemsController#part_tag as HTML
Parameters: {"token"=>"ccc", "stock_item_id"=>"502393374"}
User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'abc.com' LIMIT 1
↳ app/controllers/concerns/authentication.rb:58:in `in_development_mode?'
Profile Load (1.0ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 185443385 LIMIT 1
↳ app/models/ability.rb:10:in `block in initialize'
Permission Load (1.4ms) SELECT DISTINCT `permissions`.* FROM `permissions` INNER JOIN `permissions_profiles` ON `permissions`.`id` = `permissions_profiles`.`permission_id` WHERE `permissions_profiles`.`profile_id` = 185443385
↳ app/models/ability.rb:10:in `block in initialize'
StockItem Load (1.1ms) SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 502393374 LIMIT 1
↳ app/controllers/stock_items_controller.rb:104:in `part_tag'
User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1
↳ app/controllers/stock_items_controller.rb:105:in `part_tag'
Rendering layouts/application.html.erb
Rendering stock_items/part_tag.html.erb within layouts/application
Product Load (1.6ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 792659304 LIMIT 1
↳ app/views/stock_items/_serviceable_part_tag.html.erb:10
Certificate Load (1.0ms) SELECT `certificates`.* FROM `certificates` WHERE `certificates`.`stock_item_id` = 502393374
↳ app/views/stock_items/_serviceable_part_tag.html.erb:60
Transfer Load (1.7ms) SELECT `transfers`.* FROM `transfers` WHERE `transfers`.`id` = 273907479 LIMIT 1
↳ app/views/stock_items/_serviceable_part_tag.html.erb:71
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 207907133 LIMIT 1
↳ app/views/stock_items/_serviceable_part_tag.html.erb:71
Rendered stock_items/_serviceable_part_tag.html.erb (Duration: 39.2ms | Allocations: 7894)
Rendered stock_items/_unserviceable_part_tag.html.erb (Duration: 0.1ms | Allocations: 35)
Rendered stock_items/part_tag.html.erb within layouts/application (Duration: 46.1ms | Allocations: 10204)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_nav_bar_common_option.html.erb (Duration: 14.0ms | Allocations: 281)
Rendered layouts/_header.html.erb (Duration: 32.5ms | Allocations: 443)
Rendered layouts/_nav_bar_common_option.html.erb (Duration: 0.6ms | Allocations: 276)
Rendered layouts/_focused_object_header.html.erb (Duration: 1.6ms | Allocations: 422)
Rendered layouts/_footer.html.erb (Duration: 0.2ms | Allocations: 51)
Rendered layouts/application.html.erb (Duration: 407.8ms | Allocations: 142784)
Completed 200 OK in 458ms (Views: 405.1ms | ActiveRecord: 11.1ms | Allocations: 151063)
Started GET "/stock_items/747718251/part_tag?amp;token=cccc" for ::1 at 2020-11-09 19:25:14 -0500
Processing by StockItemsController#part_tag as HTML
Parameters: {"token"=>"ccc", "stock_item_id"=>"747718251"}
User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1
↳ app/controllers/concerns/authentication.rb:58:in `in_development_mode?'
Profile Load (0.7ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 185443385 LIMIT 1
↳ app/models/ability.rb:10:in `block in initialize'
Permission Load (1.0ms) SELECT DISTINCT `permissions`.* FROM `permissions` INNER JOIN `permissions_profiles` ON `permissions`.`id` = `permissions_profiles`.`permission_id` WHERE `permissions_profiles`.`profile_id` = 185443385
↳ app/models/ability.rb:10:in `block in initialize'
StockItem Load (0.9ms) SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 747718251 LIMIT 1
↳ app/controllers/stock_items_controller.rb:104:in `part_tag'
User Load (1.4ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1
↳ app/controllers/stock_items_controller.rb:105:in `part_tag'
Rendering layouts/application.html.erb
Rendering stock_items/part_tag.html.erb within layouts/application
Product Load (1.0ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 792659304 LIMIT 1
↳ app/views/stock_items/_serviceable_part_tag.html.erb:10
Certificate Load (1.0ms) SELECT `certificates`.* FROM `certificates` WHERE `certificates`.`stock_item_id` = 747718251
↳ app/views/stock_items/_serviceable_part_tag.html.erb:60
Transfer Load (0.7ms) SELECT `transfers`.* FROM `transfers` WHERE `transfers`.`id` = 1026987850 LIMIT 1
↳ app/views/stock_items/_serviceable_part_tag.html.erb:71
User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 207907133 LIMIT 1
↳ app/views/stock_items/_serviceable_part_tag.html.erb:71
Rendered stock_items/_serviceable_part_tag.html.erb (Duration: 22.2ms | Allocations: 7229)
Rendered stock_items/_unserviceable_part_tag.html.erb (Duration: 0.1ms | Allocations: 35)
Rendered stock_items/part_tag.html.erb within layouts/application (Duration: 30.8ms | Allocations: 10908)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_nav_bar_common_option.html.erb (Duration: 0.3ms | Allocations: 281)
Rendered layouts/_header.html.erb (Duration: 0.9ms | Allocations: 479)
Rendered layouts/_nav_bar_common_option.html.erb (Duration: 0.3ms | Allocations: 276)
Rendered layouts/_focused_object_header.html.erb (Duration: 0.6ms | Allocations: 422)
Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 51)
Rendered layouts/application.html.erb (Duration: 283.9ms | Allocations: 136084)
Completed 200 OK in 328ms (Views: 281.3ms | ActiveRecord: 8.9ms | Allocations: 144078)
**Rendering text template
Rendered text template (Duration: 0.0ms | Allocations: 1)
Sent data Serviceable_JBAEA00969_11-09-2020.pdf (2.1ms)**
Completed 200 OK in 6688ms (Views: 1.8ms | ActiveRecord: 4.7ms | Allocations: 2706188)
Rendering text template
Rendered text template (Duration: 0.0ms | Allocations: 1)
Sent data Serviceable_JBAEA00967_11-09-2020.pdf (1.3ms)
Completed 200 OK in 6596ms (Views: 1.1ms | ActiveRecord: 3.9ms | Allocations: 2582540)
Комментарии:
1. Загрузка файла ничем не отличается от загрузки HTML-документа. С точки зрения браузера, это запрос GET к ресурсу, который возвращает PDF. Что происходит, когда вы ссылаетесь на несколько «обычных HTML» ссылок одновременно? Точно так же, как вы описали, отображается только последняя страница. Такое поведение ожидается и является поведением браузеров по умолчанию, обрабатывающих ссылки.
2. Есть ли какое-либо решение для решения этой проблемы?
3. Вы ничего не можете сделать или настроить в Ruby, чтобы заставить это работать. IMO единственный вариант — щелкнуть правой кнопкой мыши и выбрать открыть в новом кране / окне . Или какой-нибудь Javascript, который в основном делает то же самое.
4. Можно ли добавить значок загрузчика / счетчика после того, как пользователь нажмет на ссылку для загрузки, чтобы пользователь не мог параллельно нажимать на другие ссылки до завершения первой загрузки? Я пытался это сделать, но не смог с этим справиться. Я использую turbolinks, и он не запускается при нажатии на ссылку.