Unexpected token when trying to upload image

810 views
Skip to first unread message

Alex Deering

unread,
Oct 3, 2016, 1:04:42 AM10/3/16
to Shrine
Im trying to get shrine to work to upload an image and when I submit the form I get a json parse error

JSON::ParserError in RecipesController#update
743:unexpected token at'shakshuka.jpg'

Here is my ImageUploader:

require "image_processing/mini_magick"

class ImageUploader < Shrine

include ImageProcessing::MiniMagick

  plugin :determine_mime_type
  plugin :logging, logger: Rails.logger
  plugin :remove_attachment
  plugin :store_dimensions
  plugin :versions, names: [:original, :thumb]
  plugin :backgrounding

  def process(io, context)
        case context[:phase]
        when :store
      thumb = resize_to_limit!(io.download, 200, 200)
      { original: io, thumb: thumb }
        end
  end
end

And my recipes controller update function:
def update
    respond_to do |format|
      if @recipe.update(recipe_params)
        format.html { redirect_to @recipe, notice: 'Recipe was successfully updated.' }
        format.json { render :show, status: :ok, location: @recipe }
      else
        format.html { render :edit }
        format.json { render json: @recipe.errors, status: :unprocessable_entity }
      end
    end
  end

My Shrine initializer:

require "shrine"
require "shrine/storage/s3"

s3_options = {
  access_key_id:     ENV.fetch('AWS_ACCESS_KEY_ID'),
  secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
  region:            'us-west-2',
  bucket:            ENV.fetch('S3_BUCKET_NAME')
}

Shrine.storages = {
  cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
  store: Shrine::Storage::S3.new(prefix: "store", **s3_options),
}

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data

Ive added the uploader in my model like this:
include ImageUploader[:image]


I think that is everything involved but not sure what is causing the error

Janko Marohnić

unread,
Oct 3, 2016, 1:21:57 AM10/3/16
to Alex Deering, Shrine
Hey Alex,

This error looks like that it's caused by the HTML form not having the enctype="multipart/form-data" attribute, could that be the issue? Rails' form builder should automatically add it for you when you add a file field, but if you're writing the form explicitly you have to add it directly.

Kind regards,
Janko
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/ec594788-7bcc-4fcb-bead-273845896aa2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Deering

unread,
Oct 3, 2016, 1:26:04 AM10/3/16
to Shrine
Ok, I was using simple form and forgot to add the multipart on there.  However now I get this error:

The `file` command-line tool is not installed

Janko Marohnić

unread,
Oct 3, 2016, 2:45:51 AM10/3/16
to Alex Deering, Shrine
This is coming from the determine_mime_type plugin. By default it tries to use the `file` command-line utility, which is installed in most OSes, but in your case it seems to be missing.

You could either try to install it, or tell the determine_mime_type plugin to use the MimeMagic gem instead (in that case you need to add `gem "mimemagic"` to your Gemfile):

plugin :determine_mime_type, analyzer: :mimemagic

Kind regards,
Janko

Alex Deering

unread,
Oct 3, 2016, 10:05:15 AM10/3/16
to Shrine
Thanks for the quick replies...for now I just removed that plugin as I dont need it, but will look into figuring out why I dont have the file command-line utility (im working in a virtual machine so that might be why)

mmari...@gmail.com

unread,
Oct 9, 2016, 5:02:32 PM10/9/16
to Shrine, deer...@gmail.com
I, too am having a similar error: 784: unexpected token at '#<ImageUploader::UploadedFile:0x007fecf2080520>'

This happens when I try to update the model without uploading an image. Here's a full stack trace: http://pastebin.com/XkWkiVUN. The data that gets passed to the avatar param: "avatar"=>"#<ImageUploader::UploadedFile:0x007fecf2080520>"},

 <%= f.input :avatar, as: :hidden, value: @user.cached_avatar_data %>
 <%= f.input :avatar, as: :file %>

I'm using simple_form and the form has the multipart attr. 

Janko Marohnić

unread,
Oct 10, 2016, 12:34:19 AM10/10/16
to mmari...@gmail.com, Shrine, Alex Deering
SimpleForm actually accepts the value through the `:input_html` option:

<%= f.input :avatar, as: :hidden, input_html: {value: @user.cached_avatar_data} %>

Since SimpleForm didn't detect the given value, it went ahead and used the value that `@photo.image` returns, which is a Shrine::UploadedFile object, and converted it into a string.

Kind regards,
Janko

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.

To post to this group, send email to ruby-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages