skip to Main Content
Technology
JSON::ParserError with RailsAdmin
Director of Engineering
May 21 2019

I ran into a JSON parsing issue when using RailsAdmin 1.4.2.

I was trying to search for an entity to associate it with another entity, but nothing was being returned. I looked at the request/response, and it was returning 500. I was able to use the search bar in other areas of the rails admin, so the general search functionality was working fine.

I dove into it a bit further and it looked like some of the fields of the entity that I was trying to search were being coerced into JSON. I wasn’t sure why that was happening (this is a rails 5.2 app running against PostgreSQL, so pretty standard). The one thing that was a bit off is that this entity had no name attribute (but it did have a title attribute).

The solution ended up being to add this bit of configuration to the rails admin initializer.

config.model Entity do
  list do
    field :name do
      searchable :title
    end
  end
end

This specifies that we’re going to search title for the name field and only this field. Which means that the other fields were no longer being coerced.

Here’s the error I was seeing in the logs.

JSON::ParserError (784: unexpected token at 'pla'):

json (1.8.6) lib/json/common.rb:155:in `parse'
json (1.8.6) lib/json/common.rb:155:in `parse'
rails_admin (1.4.2) lib/rails_admin/config/fields/types/json.rb:25:in `parse_value'
rails_admin (1.4.2) lib/rails_admin/abstract_model.rb:114:in `parse_field_value'
rails_admin (1.4.2) lib/rails_admin/adapters/active_record.rb:123:in `block in query_scope'
rails_admin (1.4.2) lib/rails_admin/adapters/active_record.rb:122:in `each'
rails_admin (1.4.2) lib/rails_admin/adapters/active_record.rb:122:in `query_scope'
rails_admin (1.4.2) lib/rails_admin/adapters/active_record.rb:33:in `all'
rails_admin (1.4.2) app/controllers/rails_admin/main_controller.rb:131:in `get_collection'
rails_admin (1.4.2) app/controllers/rails_admin/main_controller.rb:37:in `list_entries'
rails_admin (1.4.2) lib/rails_admin/config/actions/index.rb:30:in `block (2 levels) in <class:Index>'
rails_admin (1.4.2) app/controllers/rails_admin/main_controller.rb:22:in `instance_eval'
rails_admin (1.4.2) app/controllers/rails_admin/main_controller.rb:22:in `index'
actionpack (5.2.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (5.2.3) lib/abstract_controller/base.rb:194:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.2.3) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (5.2.3) lib/active_support/callbacks.rb:132:in `run_callbacks'
actionpack (5.2.3) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `instrument'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
activerecord (5.2.3) lib/active_record/railties/controller_runtime.rb:24:in `process_action'
actionpack (5.2.3) lib/abstract_controller/base.rb:134:in `process'
actionview (5.2.3) lib/action_view/rendering.rb:32:in `process'
actionpack (5.2.3) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (5.2.3) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:34:in `serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:840:in `call'
railties (5.2.3) lib/rails/engine.rb:524:in `call'
railties (5.2.3) lib/rails/railtie.rb:190:in `public_send'
railties (5.2.3) lib/rails/railtie.rb:190:in `method_missing'
actionpack (5.2.3) lib/action_dispatch/routing/mapper.rb:19:in `block in <class:Constraints>'
actionpack (5.2.3) lib/action_dispatch/routing/mapper.rb:48:in `serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:840:in `call'
rack-pjax (1.1.0) lib/rack/pjax.rb:12:in `call'

Back To Top
×Close search
Search