[英]views taking long time to render in rails 4 with public_activity
进入索引页面大约需要6秒钟的时间,考虑到开发环境中目前只有4个用户在我的应用程序上注册,因此这太长了。 我正在使用Rails 4,devise,cancancan,public_activity,acts_as_commentable_through_threading,simple_form,haml。
这是服务器的一些输出
Rendered dogs/_activity.html.haml (5726.5ms)
Rendered dogs/index.html.haml within layouts/application (6033.2ms)
index.html.haml
= provide(:title, @dog.name)
%h1 Dog Park
.row
.col-xs-10.col-xs-offset-1.center
.grass(style="margin-bottom:20px")
.container
.row
.col-sm-3.col-sm-offset-1
= render 'dogs/home_info'
.col-sm-6
= render 'dogs/activity'
_activity.html.haml(使用公共活动)
%section
= render 'post_form'
#activities
- @activities.each do |activity|
.activity= render_activity activity
%ul.pager
%li= link_to_previous_page @activities, "Newer"
%li= link_to_next_page @activities, "Older"
然后,我有3个活动public_activity / post / _create.html.haml
- if activity.trackable
-# unless activity.trackable.foreign? and current_master == @master and Dog.find(activity.owner_id).master == current_master
.panel.panel-info{id: "activity#{activity.id}"}
.panel-heading
= link_to "×", master_dog_post_path(@master, activity.owner, Post.find(activity.trackable_id), :activity_id => activity.id), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to delete this Post?", disable_with: "×" }, :class => 'close'
- if activity.owner
= link_to master_dog_path(activity.owner.master, activity.owner) do
- if activity.owner.profile_pics.empty?
= image_tag("dog_silhouette.jpg", alt: "Dog Profile pic", class: "img-circle", size: "50")
- else
= image_tag(activity.owner.profile_pics.first.image.thumbnail.url, :width => 50, :height => 50, :crop => :fill, class: "img-circle")
= activity.owner.name
updated their doggy status
= time_ago_in_words(activity.created_at)
ago.
%div(style="padding: 20px 10px;")
= activity.trackable.content
.panel-footer(style="margin-top: 0px")
.small
Comments
%div{ id: "comments#{activity.id}" }
= render :partial => 'comments/comment', collection: activity.comment_threads , as: :comment, locals: { object: "Dog"}
%div{ id: "form#{activity.id}" }
= render :partial => 'comments/form', locals: { activity: activity }
public_activity / post_pic / _create.html.haml
- if activity.trackable
-# unless activity.trackable.foreign? and current_master == @master and Dog.find(activity.owner_id).master == current_master
.panel.panel-info{id: "activity#{activity.id}"}
.panel-heading
= link_to "×", master_dog_post_pic_path(activity.owner.master, activity.owner, PostPic.find(activity.trackable_id), :activity_id => activity.id), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to delete this Picture?", disable_with: "×" }, :class => 'close'
- if activity.owner
= link_to master_dog_path(activity.owner.master, activity.owner) do
- if activity.owner.profile_pics.empty?
= image_tag("dog_silhouette.jpg", alt: "Dog Profile pic", class: "img-circle", size: "50")
- else
= image_tag(activity.owner.profile_pics.first.image.thumbnail.url, :width => 50, :height => 50, :crop => :fill, class: "img-circle")
= activity.owner.name
added a picture
= time_ago_in_words(activity.created_at)
ago.
.list-group
- if activity.trackable.title?
%li.list-group-item
= activity.trackable.title
%li.list-group-item.center(style= "padding: 20px 0px;")
= image_tag(activity.trackable.image.url, :width => 5000, :height => 400, :crop => :fill, class: "img-thumbnail center")
- if activity.trackable.description?
%li.list-group-item
= activity.trackable.description
.panel-footer
%div{ id: "comments#{activity.id}" }
.small Comments
= render :partial => 'comments/comment', collection: activity.comment_threads , as: :comment, locals: { object: "Dog"}
%div{ id: "form#{activity.id}" }
= render :partial => 'comments/form', locals: { activity: activity }
public_activity / profile_pic / _create.html.haml
- if activity.trackable
.panel.panel-info{id: "activity#{activity.id}"}
.panel-heading
- if activity.owner
- if activity.owner_type == "Dog"
= link_to "×", master_dog_profile_pic_path(activity.owner.master, activity.owner, ProfilePic.find(activity.trackable_id), :activity_id => activity.id), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to delete this profile picture?", disable_with: "×" }, :class => 'close'
= link_to master_dog_path @master, activity.owner do
= image_tag(activity.owner.profile_pics.first.image.thumbnail.url, :width => 50, :height => 50, :crop => :fill, class: "img-circle")
= activity.owner.name
- else
= link_to "×", master_profile_pic_path(activity.owner, ProfilePic.find(activity.trackable_id), :activity_id => activity.id), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to delete this profile picture?", disable_with: "×" }, :class => 'close'
= link_to master_path activity.owner do
= image_tag(activity.owner.profile_pics.first.image.thumbnail.url, :width => 50, :height => 50, :crop => :fill, class: "img-circle")
= activity.owner.name
updated their profile picture
= time_ago_in_words(activity.created_at)
ago.
.center(style="padding: 20px 0")
- if activity.owner_type === "Dog"
= image_tag(activity.trackable.image.thumbnail.url, :width => 300, :height => 200, :crop => :fill, class: "img-thumbnail center")
- else
= image_tag activity.trackable.image.thumbnail.url, :width => 300, :height => 200, :crop => :fill, class: "img-thumbnail center"
.panel-footer
%div{ id: "comments#{activity.id}" }
.small Comments
- if activity.owner_type === "Dog"
= render :partial => 'comments/comment', collection: activity.comment_threads , as: :comment, locals: { object: "Dog"}
- else
= render :partial => 'comments/comment', collection: activity.comment_threads , as: :comment, locals: { object: "Master"}
%div{ id: "form#{activity.id}" }
= render :partial => 'comments/form', locals: { activity: activity }
我的评论是通过acts_as_commentable_through_threading
comments / comment.html.haml
%div{ id: "comment#{comment.id}" }
%hr(style="margin: 5px")
= link_to "×", comment_path(comment), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to remove this comment?", disable_with: "×" }, :class => 'close'
- if Dog.exists?(comment.user_id)
= link_to master_dog_path Dog.find(comment.user_id).master, Dog.find(comment.user_id) do
- if Dog.find(comment.user_id).profile_pics.empty?
= image_tag("dog_silhouette.jpg", alt: "Dog Profile pic", class: "img-circle", size: "30")
- else
= image_tag(Dog.find(comment.user_id).profile_pics.first.image.thumbnail.url, :width => 30, :height => 30, :crop => :fill, class: "img-circle")
= Dog.find(comment.user_id).name
%inline.small
= time_ago_in_words(comment.created_at)
ago.
%p.small= comment.body
comments / _form.html.haml
= simple_form_for Comment.new, :remote => true do |f|
= f.input :body, :input_html => { :rows => "2",placeholder: "Add a comment..." }, :label => false
= f.input :commentable_id, :as => :hidden, :input_html => { :value => activity.id }
= f.input :commentable_type, :as => :hidden, :input_html => { :value => activity }
= f.button :submit, "Comment", :class => "btn btn-primary", :disable_with => "Adding Comment…"
这是动作。 我通过keep_if块获取ID,然后在where方法中使用它来获取@activities,我确定这可能效率低下,但是我有点新,并且不确定如何在where方法中请求如此复杂的查询。 如果有人对更有效的方法有任何建议,我将不胜感激。
def index
@dog = current_dog
@master = current_master
@buddy_ids = @master.buddy_ids
@master_id = @master.id
@buddy_dog_ids = []
@master.buddies.each do |b|
@buddy_dog_ids << b.dog_ids
end
@master_dog_ids = @master.dog_ids
@activity_ids = PublicActivity::Activity.ids.keep_if do |id|
a = PublicActivity::Activity.find(id)
a.trackable_type.constantize.exists?(a.trackable_id) and
#take out current_master.dogs posts and pics
(@master_dog_ids.include?(a.owner_id) or @master_dog_ids.include?(a.recipient_id) and (a.owner_type == "Dog")) or
#take out buddies.dogs posts and pics (not foreign)
(@buddy_dog_ids.flatten.include?(a.owner_id) and a.trackable_type.constantize.exists?(a.trackable_id) and !(a.trackable_type.constantize.find(a.trackable_id).foreign? unless a.trackable_type == "ProfilePic") and (a.owner_type == "Dog")) or
#take out profile pics
(@master_id == a.owner_id or @buddy_ids.include?(a.owner_id) and a.owner_type == "Master")
end
@activities = PublicActivity::Activity.order("created_at desc").where(id: @activity_ids).page(params[:page]).per(50)
@post = @dog.posts.build
@photo = @dog.post_pics.build
@new_comment = Comment.new
结束
缓存将作为临时解决方案进行。 不过,从长远来看,您应该尝试使代码更高效。
在_activity.html.haml中,不要使用@activities实例变量,而是试图一次又一次显示所有活动。
你最好写:
.activity= render_activity activity
就是这样,没有别的! 因为_activity部分在这里仅呈现一个活动,所以没有很多活动。 你能告诉我你的index.html.haml吗?
尝试:
#index.html.haml
= provide(:title, @dog.name)
%h1 Dog Park
.row
.col-xs-10.col-xs-offset-1.center
.grass(style="margin-bottom:20px")
.container
.row
.col-sm-3.col-sm-offset-1
= render 'dogs/home_info'
.col-sm-6
%section
= render 'post_form'
#activities
= render @activities
%ul.pager
%li= link_to_previous_page @activities, "Newer"
%li= link_to_next_page @activities, "Older"
和:
#activities/_activity.html.haml
.activity= render_activity activity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.