我使用导轨 5
我要统计项目的浏览量
我安装了gem 'impressionist'
生成印象表迁移
rails g impressionist
运行迁移
rake db:migrate
在视图项目/show.html.erb
<%= @project.impressionist_count %>
在 <%= @project.impressionist_count %> 错误数量的参数(给定 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
项目控制器
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