@ project.impressionist_count - 错误的参数个数(给定2,预期0..1)

时间:2016-06-01 05:47:33

标签: ruby ruby-on-rails-5 impressionist

我使用rails 5

我想计算项目的观看次数

我安装了gem 'impressionist'

生成展示次数表格迁移

rails g impressionist

运行迁移

rake db:migrate

在视图项目/ show.html.erb

<%= @project.impressionist_count %>

在线显示错误&lt;%= @ project.impressionist_count%&gt;错误的参数数量(给定2,预期0..1)我无法确定错误原因。

日志:

app/views/projects/show.html.erb:89:in `_app_views_projects_show_html_erb___149972252570834158_69970117978380'
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (8.9ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.1ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (121.7ms)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from status_code_with_paginate at /usr/local/rvm/gems/ruby-2.3.0/gems/will_paginate-3.1.0/lib/will_paginate/railtie.rb:49)

在rails控制台中

pry(main)> project.impressionist_count
ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0.rc1/lib/active_record/associations/collection_proxy.rb:731:in `count'

模型项目

class Project < ActiveRecord::Base
 ..........................

  is_impressionable

.............................

end

ProjectsController

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update]
  before_action :authenticate_user!, only: [:new, :edit] 
  before_action :require_permission, only: [:edit]
  before_action :set_freelancer, only: [:show]
  before_action :set_response_current_user, only: :show, if:  :exist_response?
  after_action :verify_authorized, only: [:update]  

  impressionist :actions => [:show]

    def show
      @new_response = @project.responses.build
      impressionist(@project)
    end
...............................................
end

1 个答案:

答案 0 :(得分:0)

您的模型应该是:

class Project < ActiveRecord::Base
 ..........................
  include Impressionist::IsImpressionable
  is_impressionable

 .............................

end

控制器

def show
  @new_response = @project.responses.build
  # impressionist(@project) //comment out this line
 end