【问题标题】:views taking long time to render in rails 4 with public_activity使用 public_activity 在 Rails 4 中需要很长时间才能呈现的视图
【发布时间】:2014-03-21 08:41:42
【问题描述】:

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

我的 cmets 是通过acts_as_commentable_through_threading

cmets/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

cmets/_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

结束

【问题讨论】:

  • 哇,昨天也遇到了同样的问题,我们在一个项目中也使用了public_activity

标签: ruby-on-rails acts-as-commentable public-activity


【解决方案1】:

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

【讨论】:

  • 好的,谢谢,我会阅读关于缓存的页面。有什么想法可以让我的代码更高效吗?谢谢
【解决方案2】:

在您的 _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 

【讨论】:

  • 好的,我会尝试删除@activities,然后将 index.html.haml 放在上面。谢谢
  • 是的,把活动拿出来只会让它崩溃。我很确定它需要针对范围和提供集合的活动。
  • 等一下,也许我不明白你在做什么,你想要每个活动的表格?
  • 好的,我去掉了多余的部分,它仍然显示 Completed 200 OK in 4663ms(查看:4178.3ms | ActiveRecord:41.7ms | Solr:0.0ms)。有趣的是,每次我刷新索引页面时,它都会上升大约 100.ms。对于每个活动的最后一个问题,我想显示活动并添加一个 cmets 表单,其下方有一个 cmets 列表。
  • 你的意思是单行文件?尝试逐行注释以找到那么长的行...
猜你喜欢
  • 2012-12-04
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多