NoMethodError(#<Event:0x6042e88> 的未定义方法“空?”):

NoMethodError (undefined method `empty?' for #<Event:0x6042e88>):

我使用 postgre 数据库创建了一个 rails API,其中我有一个模型 (table) 名称为 counseling_event

注意:不使用脚手架并使用rails-5

我的 counseling_event 架构师(迁移)如下:

class CreateCounselingEvent < ActiveRecord::Migration[5.0]
def change
create_table :counseling_event do |t|
  t.text :name
  t.datetime :start_time
  t.datetime :end_time
  t.text :location
  t.integer :user_id
  t.integer :role_id
  t.timestamps
 end
 end
end

我在上面table的基础上加了一列,是enum类型的。列名是 event_type

现在,在我的控制器中,即 CounselingEventController,在我的操作索引中,我正在从 counseling_event table 中检索数据。代码如下

def index
  @counseling_event = CounselingEvent.where(start_time: "2016-10-30".."2016-12-11")
  render body: @counseling_event
end

当我通过以下路由从前端调用此 api 控制器操作时

get 'counseling_event/index'

我的控制台出现此错误:

CounselingEvent Load (1.5ms)  SELECT "counseling_event".* FROM "counseling_event"

    NoMethodError (undefined method `empty?' for #<CounselingEvent:0x47d1d58>):

    activemodel (5.0.0.1) lib/active_model/attribute_methods.rb:433:in `method_missing'
    rack (2.0.1) lib/rack/etag.rb:68:in `block in digest_body'
    activerecord (5.0.0.1) lib/active_record/relation/delegation.rb:38:in `each'
    activerecord (5.0.0.1) lib/active_record/relation/delegation.rb:38:in `each'
    actionpack (5.0.0.1) lib/action_dispatch/http/response.rb:145:in `each_chunk'
    actionpack (5.0.0.1) lib/action_dispatch/http/response.rb:126:in `each'
    actionpack (5.0.0.1) lib/action_dispatch/http/response.rb:74:in `each'
    actionpack (5.0.0.1) lib/action_dispatch/http/response.rb:464:in `each'
    rack (2.0.1) lib/rack/etag.rb:66:in `digest_body'
    rack (2.0.1) lib/rack/etag.rb:29:in `call'
    rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
    rack (2.0.1) lib/rack/head.rb:12:in `call'
    activerecord (5.0.0.1) lib/active_record/migration.rb:552:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
    activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
    activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
    activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
    railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
    railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
    activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
    activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
    railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
    rack (2.0.1) lib/rack/runtime.rb:22:in `call'
    activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
    rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
    rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
    railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
    puma (3.6.0) lib/puma/configuration.rb:225:in `call'
    puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
    puma (3.6.0) lib/puma/server.rb:415:in `process_client'
    puma (3.6.0) lib/puma/server.rb:275:in `block in run'
    puma (3.6.0) lib/puma/thread_pool.rb:116:in `call'
    puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

我不知道错误的原因,但正在努力解决它。谁能帮我解决这个错误。似乎是一些活动模型错误。提前致谢。

Rails 基于 Rack。 Rack 只能处理字符串。您正在渲染一个对象,而不是一个字符串。

你可能想要这样的东西:

render plain: @counceling_event.to_s

这基本上是因为 Rack 试图在响应主体上调用 #empty? 以确定它应该如何响应客户端。

但是由于您的 @counceling_event 不是字符串并且不响应 empty? 它失败了。

我不确定 render body: <SOMETHING> 你想做什么。如果您使用的是 Rails 应用程序,您可能希望直接从控制器呈现视图而不是内容。

如果你真的想从控制器渲染你应该考虑使用 render plain: <SOMETHING>

希望对您有所帮助。