2015-08-26 83 views
0

我在RoR中有點新,我遇到了一些問題,希望你們能幫助我。使用rails和dropbox下載鏈接文件的渲染視圖

在控制器:

def process 
    @order = Order.includes({folders: :document}, {folders: :service}).find(params[:order_id]) 
end 

在視圖:

[email protected] do |folder| 

    p 
     b one: 
     | #{folder.service[:title]}_#{folder.service[:description]}_# {folder.service[:ink]}   

     b two: 
     | #{folder[:amount]} 

    h3 three 
    [email protected][0][:description] 

    h3 four 
    [email protected][0].document.each do |document| 
     a[href= "#{document.file.url(download: true)}"] #{document[:file_file_name]} 

這個作品 「精」。它顯示完美的下載鏈接,但視圖渲染需要很長時間。

這裏是日誌與2鏈接文件:

Started GET "/orders/ver_detalles?order_id=297" for 127.0.0.1 at 2015-08-25 22:17:42 -0430 
Processing by OrdersController#ver_detalles as JS 
    Parameters: {"order_id"=>"297"}`enter code here` 
    [1m[36mUser Load (0.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 9 ORDER BY `users`.`id` ASC LIMIT 1[0m 
    [1m[35mOrder Exists (0.0ms)[0m SELECT 1 AS one FROM `orders` WHERE `orders`.`id` = 297 LIMIT 1 
    [1m[36mOrder Load (0.0ms)[0m [1mSELECT `orders`.* FROM `orders` WHERE `orders`.`id` = 297 LIMIT 1[0m 
    [1m[35mFolder Load (0.0ms)[0m SELECT `folders`.* FROM `folders` WHERE `folders`.`order_id` IN (297) 
    [1m[36mDocument Load (0.0ms)[0m [1mSELECT `documents`.* FROM `documents` WHERE `documents`.`folder_id` IN (326)[0m 
    [1m[35mService Load (0.0ms)[0m SELECT `services`.* FROM `services` WHERE `services`.`id` IN (1) 
    Rendered partials/_detallesOrden.html.slim (4703.1ms) 
    Rendered orders/ver_detalles.js.erb (4718.8ms) 
Completed 200 OK in 4750ms (Views: 4734.4ms | ActiveRecord: 0.0ms) 

這裏是日誌有4個鏈接文件:

Started GET "/orders/ver_detalles?order_id=303" for 127.0.0.1 at 2015-08-25 22:13:31 -0430 
Processing by OrdersController#ver_detalles as JS 
    Parameters: {"order_id"=>"303"} 
    [1m[36mUser Load (0.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 9 ORDER BY `users`.`id` ASC LIMIT 1[0m 
    [1m[35mOrder Exists (15.6ms)[0m SELECT 1 AS one FROM `orders` WHERE `orders`.`id` = 303 LIMIT 1 
    [1m[36mOrder Load (0.0ms)[0m [1mSELECT `orders`.* FROM `orders` WHERE `orders`.`id` = 303 LIMIT 1[0m 
    [1m[35mFolder Load (0.0ms)[0m SELECT `folders`.* FROM `folders` WHERE `folders`.`order_id` IN (303) 
    [1m[36mDocument Load (0.0ms)[0m [1mSELECT `documents`.* FROM `documents` WHERE `documents`.`folder_id` IN (332, 333)[0m 
    [1m[35mService Load (0.0ms)[0m SELECT `services`.* FROM `services` WHERE `services`.`id` IN (7, 4) 
    Rendered partials/_detallesOrden.html.slim (8453.1ms) 
    Rendered orders/ver_detalles.js.erb (8453.1ms) 
Completed 200 OK in 8500ms (Views: 8484.4ms | ActiveRecord: 15.6ms) 

所以當查詢返回多個文件的渲染需要更多的時間。如果我評論視圖中的最後一行,則渲染只需要60ms。我怎樣才能解決這個問題?

def process 
     @order = Order.includes(folders: [:document, :service]).find(params[:order_id]) 
    end 

看起來你的諧音正在做一些重型的工作,他們正在採取的最長時間才能完成請求所需:

回答

0

首先,你可以使用下面的語法包括他們喜歡這種關聯:

Rendered partials/_detallesOrden.html.slim (8453.1ms) 
    Rendered orders/ver_detalles.js.erb (8453.1ms) 

您的@order.folders[0].document.each循環中的每一行都會承受部分加載開銷。所以,文檔的大小越大,渲染視圖所需的時間就越長。

我建議你尋找緩存策略,如fragment caching,如果你可以在你的部分使用。這將爲您節省大量時間和開銷。

+0

嗨,謝謝你的回答。我嘗試片段緩存但是是相同的,視圖需要很多時間渲染。 –

+0

如果您循環大量文件,則需要時間。因此,您必須想出一些替代方案,並瞭解如何最大限度地減少視圖中的繁重工作,例如,查看是否可以將視圖分割爲多個部分。 –