783: unexpected token at ''

Hi friends!
After upgrading my rails app to 5.2.5 my file uploads failed. Always with the message: ```
783: unexpected token at ‘’
I tried some suggestions from Google without success. Could anyone help me?

require “image_processing/mini_magick”
class ImageUploader < Shrine
plugin :determine_mime_type
plugin :remove_attachment
plugin :store_dimensions
plugin :validation_helpers
plugin :pretty_location
plugin :derivatives, create_on_promote: true

Attacher.validate do
validate_max_size 5.megabytes, message: ‘é muito grande (máximo é 5 MB)’
validate_mime_type_inclusion [‘image/jpg’, ‘image/jpeg’, ‘image/png’, ‘image/gif’]
end

Attacher.derivatives do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
size_250: magick.resize_to_limit!(250, 250),
size_80: magick.resize_to_limit!(80, 80)
}
end
end

My shrine.rb:
require “shrine”
require “shrine/storage/file_system”

Shrine.storages = {
cache: Shrine::Storage::FileSystem.new(“public”, prefix: “uploads/cache”), # temporary
store: Shrine::Storage::FileSystem.new(“public”, prefix: “uploads/store”), # permanent
}

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for forms
Shrine.plugin :restore_cached_data

My form:
<%= form_for @photo, html: {autocomplete: “off”} do |f| %>


<%= f.input :image, as: :hidden, input_html: { value: @photo.cached_image_data } %>
Imagem: <%= f.file_field :image, required: true %>

<%= f.submit 'Gravar' %>
<% end %>

Thank you very much!

Can you post the backtrace of the error and the request parameters received in the controller on upload?

Hi friend Janko!
I always worked almost alone with Ruby on Rails. I had been productive but I suffer from not having colleagues to discuss about my job. So I will be embarrassed if the “backtrace of the error” is not this:

JSON::ParserError in Photos#new
Showing /home/luiz/work/samude/app/views/photos/new.html.erb where line #22 raised:

783: unexpected token at ‘’
Extracted source (around line #22):

20 <%= f.hidden_field :classplan_id %>
21
22 <%= f.input :image, as: :hidden, input_html: { value: @photo.cached_image_data } %>
23 Imagem: <%= f.file_field :image, required: true %>
24
25

Rails.root: /home/luiz/work/samude

Application Trace | Framework Trace | Full Trace
/home/luiz/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/json/common.rb:156:in parse' /home/luiz/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/json/common.rb:156:in parse’
shrine (3.3.0) lib/shrine/plugins/column.rb:95:in load' shrine (3.3.0) lib/shrine/plugins/column.rb:79:in deserialize_column’
shrine (3.3.0) lib/shrine/plugins/column.rb:42:in load_column' shrine (3.3.0) lib/shrine/plugins/entity.rb:139:in read’
shrine (3.3.0) lib/shrine/plugins/model.rb:101:in load_model' shrine (3.3.0) lib/shrine/plugins/model.rb:63:in attacher’
shrine (3.3.0) lib/shrine/plugins/entity.rb:50:in block in define_entity_methods' shrine (3.3.0) lib/shrine/plugins/cached_attachment_data.rb:12:in block in define_model_methods’
actionview (5.2.5) lib/action_view/helpers/capture_helper.rb:41:in block in capture' actionview (5.2.5) lib/action_view/helpers/capture_helper.rb:205:in with_output_buffer’
actionview (5.2.5) lib/action_view/helpers/capture_helper.rb:41:in capture' actionview (5.2.5) lib/action_view/helpers/form_helper.rb:452:in form_for’
actionview (5.2.5) lib/action_view/template.rb:159:in block in render' activesupport (5.2.5) lib/active_support/notifications.rb:170:in instrument’
actionview (5.2.5) lib/action_view/template.rb:354:in instrument_render_template' actionview (5.2.5) lib/action_view/template.rb:157:in render’
actionview (5.2.5) lib/action_view/renderer/template_renderer.rb:54:in block (2 levels) in render_template' actionview (5.2.5) lib/action_view/renderer/abstract_renderer.rb:44:in block in instrument’
activesupport (5.2.5) lib/active_support/notifications.rb:168:in block in instrument' activesupport (5.2.5) lib/active_support/notifications/instrumenter.rb:23:in instrument’
activesupport (5.2.5) lib/active_support/notifications.rb:168:in instrument' actionview (5.2.5) lib/action_view/renderer/abstract_renderer.rb:43:in instrument’
actionview (5.2.5) lib/action_view/renderer/template_renderer.rb:53:in block in render_template' actionview (5.2.5) lib/action_view/renderer/template_renderer.rb:61:in render_with_layout’
actionview (5.2.5) lib/action_view/renderer/template_renderer.rb:52:in render_template' actionview (5.2.5) lib/action_view/renderer/template_renderer.rb:16:in render’
actionview (5.2.5) lib/action_view/renderer/renderer.rb:44:in render_template' actionview (5.2.5) lib/action_view/renderer/renderer.rb:25:in render’
actionview (5.2.5) lib/action_view/rendering.rb:103:in _render_template' actionpack (5.2.5) lib/action_controller/metal/streaming.rb:219:in _render_template’
actionview (5.2.5) lib/action_view/rendering.rb:84:in render_to_body' actionpack (5.2.5) lib/action_controller/metal/rendering.rb:52:in render_to_body’
actionpack (5.2.5) lib/action_controller/metal/renderers.rb:142:in render_to_body' actionpack (5.2.5) lib/abstract_controller/rendering.rb:25:in render’
actionpack (5.2.5) lib/action_controller/metal/rendering.rb:36:in render' actionpack (5.2.5) lib/action_controller/metal/instrumentation.rb:46:in block (2 levels) in render’
activesupport (5.2.5) lib/active_support/core_ext/benchmark.rb:14:in block in ms' /home/luiz/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/benchmark.rb:308:in realtime’
activesupport (5.2.5) lib/active_support/core_ext/benchmark.rb:14:in ms' actionpack (5.2.5) lib/action_controller/metal/instrumentation.rb:46:in block in render’
actionpack (5.2.5) lib/action_controller/metal/instrumentation.rb:87:in cleanup_view_runtime' activerecord (5.2.5) lib/active_record/railties/controller_runtime.rb:31:in cleanup_view_runtime’
actionpack (5.2.5) lib/action_controller/metal/instrumentation.rb:45:in render' actionpack (5.2.5) lib/action_controller/metal/implicit_render.rb:35:in default_render’
actionpack (5.2.5) lib/action_controller/metal/basic_implicit_render.rb:6:in block in send_action' actionpack (5.2.5) lib/action_controller/metal/basic_implicit_render.rb:6:in tap’
actionpack (5.2.5) lib/action_controller/metal/basic_implicit_render.rb:6:in send_action' actionpack (5.2.5) lib/abstract_controller/base.rb:194:in process_action’
actionpack (5.2.5) lib/action_controller/metal/rendering.rb:30:in process_action' actionpack (5.2.5) lib/abstract_controller/callbacks.rb:42:in block in process_action’
activesupport (5.2.5) lib/active_support/callbacks.rb:132:in run_callbacks' actionpack (5.2.5) lib/abstract_controller/callbacks.rb:41:in process_action’
actionpack (5.2.5) lib/action_controller/metal/rescue.rb:22:in process_action' actionpack (5.2.5) lib/action_controller/metal/instrumentation.rb:34:in block in process_action’
activesupport (5.2.5) lib/active_support/notifications.rb:168:in block in instrument' activesupport (5.2.5) lib/active_support/notifications/instrumenter.rb:23:in instrument’
activesupport (5.2.5) lib/active_support/notifications.rb:168:in instrument' actionpack (5.2.5) lib/action_controller/metal/instrumentation.rb:32:in process_action’
actionpack (5.2.5) lib/action_controller/metal/params_wrapper.rb:256:in process_action' activerecord (5.2.5) lib/active_record/railties/controller_runtime.rb:24:in process_action’
actionpack (5.2.5) lib/abstract_controller/base.rb:134:in process' actionview (5.2.5) lib/action_view/rendering.rb:32:in process’
actionpack (5.2.5) lib/action_controller/metal.rb:191:in dispatch' actionpack (5.2.5) lib/action_controller/metal.rb:252:in dispatch’
actionpack (5.2.5) lib/action_dispatch/routing/route_set.rb:52:in dispatch' actionpack (5.2.5) lib/action_dispatch/routing/route_set.rb:34:in serve’
actionpack (5.2.5) lib/action_dispatch/journey/router.rb:52:in block in serve' actionpack (5.2.5) lib/action_dispatch/journey/router.rb:35:in each’
actionpack (5.2.5) lib/action_dispatch/journey/router.rb:35:in serve' actionpack (5.2.5) lib/action_dispatch/routing/route_set.rb:840:in call’
bullet (6.1.4) lib/bullet/rack.rb:12:in call' warden (1.2.9) lib/warden/manager.rb:36:in block in call’
warden (1.2.9) lib/warden/manager.rb:34:in catch' warden (1.2.9) lib/warden/manager.rb:34:in call’
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in call' rack (2.2.3) lib/rack/etag.rb:27:in call’
rack (2.2.3) lib/rack/conditional_get.rb:27:in call' rack (2.2.3) lib/rack/head.rb:12:in call’
actionpack (5.2.5) lib/action_dispatch/http/content_security_policy.rb:18:in call' rack (2.2.3) lib/rack/session/abstract/id.rb:266:in context’
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in call' actionpack (5.2.5) lib/action_dispatch/middleware/cookies.rb:670:in call’
activerecord (5.2.5) lib/active_record/migration.rb:559:in call' actionpack (5.2.5) lib/action_dispatch/middleware/callbacks.rb:28:in block in call’
activesupport (5.2.5) lib/active_support/callbacks.rb:98:in run_callbacks' actionpack (5.2.5) lib/action_dispatch/middleware/callbacks.rb:26:in call’
actionpack (5.2.5) lib/action_dispatch/middleware/executor.rb:14:in call' actionpack (5.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:61:in call’
actionpack (5.2.5) lib/action_dispatch/middleware/show_exceptions.rb:33:in call' railties (5.2.5) lib/rails/rack/logger.rb:38:in call_app’
railties (5.2.5) lib/rails/rack/logger.rb:26:in block in call' activesupport (5.2.5) lib/active_support/tagged_logging.rb:71:in block in tagged’
activesupport (5.2.5) lib/active_support/tagged_logging.rb:28:in tagged' activesupport (5.2.5) lib/active_support/tagged_logging.rb:71:in tagged’
railties (5.2.5) lib/rails/rack/logger.rb:26:in call' sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in call’
actionpack (5.2.5) lib/action_dispatch/middleware/remote_ip.rb:81:in call' actionpack (5.2.5) lib/action_dispatch/middleware/request_id.rb:27:in call’
rack (2.2.3) lib/rack/method_override.rb:24:in call' rack (2.2.3) lib/rack/runtime.rb:22:in call’
activesupport (5.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in call' actionpack (5.2.5) lib/action_dispatch/middleware/executor.rb:14:in call’
actionpack (5.2.5) lib/action_dispatch/middleware/static.rb:127:in call' rack (2.2.3) lib/rack/sendfile.rb:110:in call’
railties (5.2.5) lib/rails/engine.rb:524:in call' puma (4.3.6) lib/puma/configuration.rb:228:in call’
puma (4.3.6) lib/puma/server.rb:713:in handle_request' puma (4.3.6) lib/puma/server.rb:472:in process_client’
puma (4.3.6) lib/puma/server.rb:328:in block in run' puma (4.3.6) lib/puma/thread_pool.rb:134:in block in spawn_thread’
Request Parameters:
{“id”=>“472”}

Excuse me if this is not you asked. Before this I tried for the first time the gem byebug procedures, but without success.
And sorry for my bad english.

Yes, that’s the backtrace :+1: It seems the error is coming from @photo.cached_image_data, and it seems to be when initializing the attacher from the model instance. This in turns parses the <attachment>_data column value, which appears to contain invalid JSON, possibly an empty string. Shrine doesn’t handle that, the column value either needs to be nil or valid JSON, so you need to figure out how did that happen.

Also, it would be very helpful if you used markdown fenced blocks for code, otherwise it’s difficult to read, as you can see yourself :wink:

And your english is good, there is nothing to apologize for :wink:

Ahhh! The default column value either needs to be nil or json! Yes! I was initializing the column with ‘’ and not null. I fixed it and everything is working again!
THANK YOU VERY MUCH!!
:+1:

1 Like