0

我的应用程序有一个User模型belongs_to :profile, :polymorphic => true。此配置文件可以是Student、或Manager,并且需要由其管理员批准才能登录系统。我采用了这种审批方式EnterpriseEmployee

我在测试部分遇到了一些麻烦。它总是输出:

1) Error:
test_should_get_dashboard(ManagersControllerTest):
NoMethodError: undefined method `approved' for nil:NilClass

可能的相关来源:

class User < ActiveRecord::Base
   belongs_to :profile, :polymorphic => true
   attr_accessible :approved
end

class ManagersControllerTest < ActionController::TestCase
  include Devise::TestHelpers
  fixtures :managers
  fixtures :users

  def setup
    manager = Manager.create(:name => 'Manager', :cpf => '537.846.919-86')
    @user = User.new(:email => 'user@user.com', :password => 'user@user.com')
    @user.approved = true
    @user.profile = manager
    @user.save!
  end

  test "should get dashboard" do
    sign_in @user
    get :dashboard
    assert_response :redirect
    assert_redirected_to managers_root_path
  end
end

class ManagersController < ApplicationController
  def dashboard
    p = Proc.new { |e| [e.name, e.id] if e.user.approved }
    app_en = Enterprise.all.map(&p).compact.count
    napp_en = Enterprise.count - app_en

    app_em = Employee.all.map(&p).compact.count
    napp_em = Employee.count - app_em

    @app = [ app_en, app_em, 0 ]
    @napp = [ napp_en, napp_em, 0 ]
    @total = [ app_en + napp_en, app_em + napp_em, Student.count ]

    tags_count = Activity.tag_counts.map(&:count)
    @tags = Hash[Activity.tag_counts.map(&:name).zip(tags_count)]

    @user = current_user
   end
end

由于我在编写固定装置时遇到问题,因此我忽略了它们,但如果有人展示固定装置的解决方案,我将非常感激。即使这样,我也陷入了这个错误。我该怎么办?


堆栈

/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:7:in `block in dashboard'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:8:in `map'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:8:in `dashboard'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:167:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/rendering.rb:10:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:425:in `_run__2212830968385571096__process_action__4077155092621945467__callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:17:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/rescue.rb:29:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `block in instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/params_wrapper.rb:206:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.6/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:121:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/rendering.rb:45:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:469:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:49:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `block in process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `catch'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `_catch_warden'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:386:in `get'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/test/functional/manager_controller_test.rb:24:in `block in <class:ManagersControllerTest>'
4

1 回答 1

0

答案似乎是并非所有 Enterprise 或 Employee 模型实际上都有关联的用户。当使用 map toe.user.approved时,控制器会尝试传递.approveduserwhich can benil在这种情况下。

一个简单的解决方案是将您的 Proc 更改为 nil 安全:

p = Proc.new { |e| [e.name, e.id] if e.user && e.user.approved }
于 2012-10-07T02:03:24.100 回答