0

访问索引页面大约需要 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

评论/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

结尾

4

2 回答 2

0

在您的 _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 
于 2014-03-21T08:52:16.360 回答
0

缓存将作为临时解决方案完成。不过,从长远来看,您应该尝试使您的代码更有效率。

于 2014-03-21T08:46:25.497 回答