NoMethodError(未定义方法`empty?' for #<Event:0x6042e88>): [英] NoMethodError (undefined method `empty?' for #<Event:0x6042e88>):

查看:51
本文介绍了NoMethodError(未定义方法`empty?' for #<Event:0x6042e88>):的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a rails API using postgre database in which i have a model (table) name as counseling_event

注意:不使用 scaffold 和使用 rails-5

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

My scheman(migration) for counseling_event is as follow:

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

我在上表中添加了一列,它是枚举类型.列名是 event_type

I added a column to the above table, which is of type enum. column name is event_type

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

Now, In my Controller i.e CounselingEventController, in my action index i'm retrieving the data from the counseling_event table. the code is as follows.

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

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

when i call this api controller action from frontend through following route

get 'counseling_event/index'

我在控制台中收到此错误:

I'm getting this error in my console:

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'

我不知道错误的原因,但是正在努力解决它.任何人都可以帮我解决这个错误.这似乎是一些 activemodel 错误.提前致谢.

I don't know the reason for the error but, trying hard to resolve it. can any one help me out to solve this error. it seems like some activemodel error. Thanks in advance.

推荐答案

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

Rails is based on Rack. Rack can only handle strings. You're rendering an object, not a string.

你可能想要这样的东西:

You probably want something like:

render plain: @counceling_event.to_s

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

It basically happens because Rack attempts to call #empty? on the body of the response to determine how it should respond to the client.

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

But since your @counceling_event isn't a string and doesn't respond to empty? it fails.

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

I'm unsure what you're trying to do by render body: <SOMETHING>. If you're on a Rails app you probably want to render a view instead of content directly from the controller.

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

If you really want to render from the controller you should consider using render plain: <SOMETHING>.

希望这会有所帮助.

这篇关于NoMethodError(未定义方法`empty?' for #&lt;Event:0x6042e88&gt;):的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆