访问索引页大约需要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 /后/ _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
评论/ 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
评论/ _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
端
答案 0 :(得分:0)
Caching将作为临时解决方案完成。但是,从长远来看,您应该尝试提高代码的效率。
答案 1 :(得分:0)
在_activity.html.haml中,不要使用@activities实例变量,而是尝试反复显示所有活动。
你最好写一下:
.activity= render_activity activity
就是这样,没有别的!因为_activity partial在这里只渲染一个活动,而不是很多活动。 你能告诉我你的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