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

社会主义新天地 提交于 2019-12-01 08:28:10

问题


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

NOTE: Not using scaffold and using rails-5

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

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

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

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'

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.


回答1:


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

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

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

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.

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

Hope this helps.



来源:https://stackoverflow.com/questions/40760620/nomethoderror-undefined-method-empty-for-event0x6042e88

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!