小编Fas*_*ian的帖子

Mongoid和counter_cache列

我有评论模型:

class Comment
  include Mongoid::Document
  include Mongoid::Timestamps
  belongs_to :commentable, polymorphic: true, touch: true#, counter_cache: true
Run Code Online (Sandbox Code Playgroud)

当我跑:

Article.first.comments.count => 1 #without counter_cache: true
Run Code Online (Sandbox Code Playgroud)

但是"counter_cache:true"我得到:

Article.first.comments.count => NoMethodError:未定义的方法count' for nil:NilClass Article.first.comments => NoMethodError: undefined method计数'为nil:NilClass

有谁遇到过这样的问题?

ruby-on-rails counter-cache mongoid

3
推荐指数
1
解决办法
1774
查看次数

如何提高rails中coffeescript的速度执行速度?

为了提高页面加载速度,我通过AJAX实现了创建注释.这很简单而不是重量级.在控制器动作中我有:

def create
    @comment = @commentable.comments.new(params_comment)
    respond_to do |format|
      if @comment.save
        flash.now[:notice] = "Your comment added."
        @response = {comment: @comment, model: @commentable}
        format.js { @response }
        format.html { redirect_to @commentable  }
      else
        format.js { render nothing: :true, status: :not_acceptable }
        format.html { redirect_to @commentable, status: :not_acceptable  }
      end
    end
  end 
Run Code Online (Sandbox Code Playgroud)

和js文件:

$("<%= escape_javascript( render 'comments/comment', @response)%>").appendTo(".comments").hide().fadeIn(500)
$('.notice-wrapper').html("<%= j(render partial: 'shared/notice') %>")
$('.alert').fadeOut(3000)

if $(".no-comments").css("display") is 'block'
  $(".no-comments").hide()
$(".new_answer").hide()
$(".open").show()
Run Code Online (Sandbox Code Playgroud)

但是,我没有加快速度,而是产生了相反的效果.通过JavaScript的响应时间增加了100-200毫秒(总共约300毫秒).这是正常的行为还是我做错了什么?有什么方法可以提高速度吗?

我的表现测试:

在此输入图像描述

UPD: 我的性能测试只用JS文件.

在此输入图像描述

javascript performance ruby-on-rails coffeescript

1
推荐指数
1
解决办法
300
查看次数

Rspec、Rails - 如何测试使用参数哈希的辅助方法?

我想实现一个类似于 stackoverflow 的标签系统,右上角有一个带有标签的框,而且我还有从 params 哈希中删除标签的链接。我的方法在浏览器中运行正常。但我找不到方法来测试它。

def tags_list_with_destroy_links
    if params[:tags]
      li = ""
      p = params[:tags].split("+") # '/tagged/sea+ship+sun' => ['sea', 'ship', 'sun']
      p.map do |t|
        remove_link = if p.count  >= 3
                        c = p.reject {|item| item == t }
                        a = c.join("+")
                        {:tags => a}
                      elsif p.count == 2
                        c = p.reject {|item| item == t }
                        {tags: c[0]}
                      else
                        questions_url
                      end

        li << content_tag(:li) do
          link_to(t, questions_tags_path(t), class: 'tag') +
              link_to( '', remove_link , class: 'icon-small icons-cross')
        end
      end

      ul …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails view-helpers

1
推荐指数
1
解决办法
3789
查看次数