填充嵌套表单时的渲染性能 - Rails 4 - OmniContacts
Rendering Performance when populating Nested Form - Rails 4 - OmniContacts
在我的应用程序中,我使用 OmniContacts gem 来允许用户导入他们的联系人。这行得通。获取 Gmail 的联系人大约需要 300-500 毫秒。
为了允许导入联系人,我将它们放在嵌套表格中。现在每个联系人的渲染需要 175-300 毫秒,将其乘以 1000,这很糟糕。
我希望有 rails 解决此问题的方法。否则我猜测使用 AJAX 并且 JS 表单可能有效。感谢您的想法和时间。
导入控制器:
def import
@user = current_user
@import = request.env['omnicontacts.contacts']
@contacts = @import.map do |contact_info|
Contact.new(
first_name: contact_info[:first_name],
last_name: contact_info[:last_name],
email_home: contact_info[:email],
phone_home: contact_info[:phone]
)
end
respond_to do |format|
format.html
end
end
导入视图:
<div class="row">
<div class="small-12">
<%= simple_form_for(@user, url: import_path) do |f| %>
<%= f.simple_fields_for :contacts, @contacts do |contact| %>
<%= render 'contact_fields', f: contact %>
<% end %>
<div class="actions" align="right">
<%= f.submit class:"button tiny" %>
</div>
<% end %>
</div>
</div>
部分嵌套表格:
<div class='nested-fields'>
<fieldset>
<div class="row">
<div class="small-2 columns">
<%= f.input :import, as: :boolean %>
</div>
<div class="small-2 columns">
<%= f.input :first_name %>
</div>
<div class="small-2 columns">
<%= f.input :last_name %>
</div>
<div class="small-4 columns">
<%= f.input :email_home %>
</div>
<div class="small-2 columns">
<%= f.input :phone_home %>
</div>
</div>
</fieldset>
</div>
厄运日志:(闭上眼睛,想象一下添加了 1000 多条渲染线)
Started GET "/oauth2callback?code=4/XzHDNPJ8RgRb7SFStQ3tVM-ff2q5WolrVHTsokI0egg" for ::1 at 2015-06-18 12:16:09 -0500
Processing by ImportsController#import as HTML
Parameters: {"code"=>"4/XzHDNPJ8RgRb7SFStQ3tVM-ff2q5WolrVHTsokI0egg"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered imports/_contact_fields.html.erb (301.0ms)
Rendered imports/_contact_fields.html.erb (233.9ms)
Rendered imports/_contact_fields.html.erb (276.8ms)
Rendered imports/_contact_fields.html.erb (180.3ms)
Rendered imports/import.html.erb within layouts/application (1001.0ms)
Rendered layouts/_header.html.erb (2.8ms)
Rendered layouts/_footer.html.erb (3.8ms)
Completed 200 OK in 1548ms (Views: 1540.0ms | ActiveRecord: 0.5ms)
原来问题是简单的形式。部分形式更改为:
<div class='nested-fields'>
<fieldset>
<div class="row">
<div class="small-1 columns">
<%= f.label "Import" %>
<%= f.check_box :import %>
</div>
<div class="small-2 columns">
<%= f.label :firs_name %>
<%= f.text_field :first_name %>
</div>
<div class="small-2 columns">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</div>
<div class="small-4 columns">
<%= f.label :email_home %>
<%= f.text_field :email_home %>
</div>
<div class="small-3 columns">
<%= f.label :phone_home %>
<%= f.text_field :phone_home %>
</div>
</div>
</fieldset>
</div>
性能提升:
Rendered imports/_contact_fields.html.erb (43.5ms)
Rendered imports/_contact_fields.html.erb (1.0ms)
Rendered imports/_contact_fields.html.erb (0.7ms)
Rendered imports/_contact_fields.html.erb (0.7ms)
Rendered imports/import.html.erb within layouts/application (407.4ms)
在我的应用程序中,我使用 OmniContacts gem 来允许用户导入他们的联系人。这行得通。获取 Gmail 的联系人大约需要 300-500 毫秒。
为了允许导入联系人,我将它们放在嵌套表格中。现在每个联系人的渲染需要 175-300 毫秒,将其乘以 1000,这很糟糕。
我希望有 rails 解决此问题的方法。否则我猜测使用 AJAX 并且 JS 表单可能有效。感谢您的想法和时间。
导入控制器:
def import
@user = current_user
@import = request.env['omnicontacts.contacts']
@contacts = @import.map do |contact_info|
Contact.new(
first_name: contact_info[:first_name],
last_name: contact_info[:last_name],
email_home: contact_info[:email],
phone_home: contact_info[:phone]
)
end
respond_to do |format|
format.html
end
end
导入视图:
<div class="row">
<div class="small-12">
<%= simple_form_for(@user, url: import_path) do |f| %>
<%= f.simple_fields_for :contacts, @contacts do |contact| %>
<%= render 'contact_fields', f: contact %>
<% end %>
<div class="actions" align="right">
<%= f.submit class:"button tiny" %>
</div>
<% end %>
</div>
</div>
部分嵌套表格:
<div class='nested-fields'>
<fieldset>
<div class="row">
<div class="small-2 columns">
<%= f.input :import, as: :boolean %>
</div>
<div class="small-2 columns">
<%= f.input :first_name %>
</div>
<div class="small-2 columns">
<%= f.input :last_name %>
</div>
<div class="small-4 columns">
<%= f.input :email_home %>
</div>
<div class="small-2 columns">
<%= f.input :phone_home %>
</div>
</div>
</fieldset>
</div>
厄运日志:(闭上眼睛,想象一下添加了 1000 多条渲染线)
Started GET "/oauth2callback?code=4/XzHDNPJ8RgRb7SFStQ3tVM-ff2q5WolrVHTsokI0egg" for ::1 at 2015-06-18 12:16:09 -0500
Processing by ImportsController#import as HTML
Parameters: {"code"=>"4/XzHDNPJ8RgRb7SFStQ3tVM-ff2q5WolrVHTsokI0egg"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered imports/_contact_fields.html.erb (301.0ms)
Rendered imports/_contact_fields.html.erb (233.9ms)
Rendered imports/_contact_fields.html.erb (276.8ms)
Rendered imports/_contact_fields.html.erb (180.3ms)
Rendered imports/import.html.erb within layouts/application (1001.0ms)
Rendered layouts/_header.html.erb (2.8ms)
Rendered layouts/_footer.html.erb (3.8ms)
Completed 200 OK in 1548ms (Views: 1540.0ms | ActiveRecord: 0.5ms)
原来问题是简单的形式。部分形式更改为:
<div class='nested-fields'>
<fieldset>
<div class="row">
<div class="small-1 columns">
<%= f.label "Import" %>
<%= f.check_box :import %>
</div>
<div class="small-2 columns">
<%= f.label :firs_name %>
<%= f.text_field :first_name %>
</div>
<div class="small-2 columns">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</div>
<div class="small-4 columns">
<%= f.label :email_home %>
<%= f.text_field :email_home %>
</div>
<div class="small-3 columns">
<%= f.label :phone_home %>
<%= f.text_field :phone_home %>
</div>
</div>
</fieldset>
</div>
性能提升:
Rendered imports/_contact_fields.html.erb (43.5ms)
Rendered imports/_contact_fields.html.erb (1.0ms)
Rendered imports/_contact_fields.html.erb (0.7ms)
Rendered imports/_contact_fields.html.erb (0.7ms)
Rendered imports/import.html.erb within layouts/application (407.4ms)