title: JSON Parser Error 783: unexpected token at for params
body: <p>I am editing a resource - <code>@article</code> - and whenever I submit it I get the following error:</p>
  
  <pre><code>JSON::ParserError in ArticlesController#update
  783: unexpected token at '#&lt;ImageUploader::UploadedFile:0x00007fb154adb508&gt;'
  </code></pre>
  
  <p>It has highlighted this line:</p>
  
  <pre><code>  if @article.update(article_params)
  </code></pre>
  
  <p>Which is the normal Rails boilerplate code in my <code>Article#Update</code> action:</p>
  
  <pre><code>  def update
      respond_to do |format|
        if @article.update(article_params)
          format.html { redirect_to @article, notice: 'Article was successfully updated.' }
          format.json { render :show, status: :ok, location: @article }
        else
          format.html { render :edit }
          format.json { render json: @article.errors, status: :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <p>Here is the full output from the server logs (including the params):</p>
  
  <pre><code>Started PATCH "/articles/welcome-to-ftja" for ::1 at 2020-05-04 02:11:43 -0500
  Processing by ArticlesController#update as HTML
    Parameters: {"authenticity_token"=&gt;"0pO56DaVEiWLX3iE3HNHRKsAPOK2z4RmUaH3jal5rOZjk7zYeoaSru9xOE9GYzLckTbnrVrz4ihXrc1hWcXzXA==", "article"=&gt;{"title"=&gt;"Welcome to FTJA!!", "featured"=&gt;"true", "image"=&gt;"#&lt;ImageUploader::UploadedFile:0x00007fb154adb508&gt;", "body"=&gt;"&lt;h1&gt;Yet another great article.&lt;/h1&gt;&lt;div&gt;Test &lt;strong&gt;bold&lt;/strong&gt; &lt;em&gt;italic&lt;/em&gt; &lt;del&gt;strikethrough&lt;br&gt;&lt;br&gt;&lt;/del&gt;&lt;figure data-trix-attachment=\"{&amp;quot;content&amp;quot;:&amp;quot;&lt;figure class=\\&amp;quot;attachment attachment--preview attachment--jpg\\&amp;quot;&gt;\\n    &lt;img src=\\&amp;quot;http://localhost:3000/rails/active_storage/representations/eyJfcJ2YXJpYXRpb24ifX0=--5b3d481c22cafd1ff8db36c1b24e57d8788d2217/cowork-4.jpg\\&amp;quot; /&gt;\\n\\n  &lt;figcaption class=\\&amp;quot;attachment__caption\\&amp;quot;&gt;\\n      Coworking.\\n  &lt;/figcaption&gt;\\n&lt;/figure&gt;\\n&amp;quot;,&amp;quot;contentType&amp;quot;:&amp;quot;image/jpeg&amp;quot;,&amp;quot;filename&amp;quot;:&amp;quot;cowork-4.jpg&amp;quot;,&amp;quot;filesize&amp;quot;:82497,&amp;quot;height&amp;quot;:768,&amp;quot;previewable&amp;quot;:true,&amp;quot;sgid&amp;quot;:&amp;quot;_ZXhwaXJlc19pbgY7AFRJIgxwdXJwb3NlBjsAVEkiD2F0dGFjaGFibGUGOwBUSSIPZXhwaXJlc19hdAY7AFQw--e56d2e422&amp;quot;,&amp;quot;url&amp;quot;:&amp;quot;http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6d89ec207ef10c8e48577a5e9a170a8d178a9338/cowork-4.jpg&amp;quot;,&amp;quot;width&amp;quot;:752}\" data-trix-content-type=\"image/jpeg\" data-trix-attributes=\"{&amp;quot;caption&amp;quot;:&amp;quot;Coworking.&amp;quot;,&amp;quot;presentation&amp;quot;:&amp;quot;gallery&amp;quot;}\" class=\"attachment attachment--content attachment--jpg\"&gt;&lt;figure class=\"attachment attachment--preview attachment--jpg\"&gt;\r\n    &lt;img src=\"http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6d89ec207ef10c8e48577a5e9a170a8d178a9338/cowork-4.jpg\" width=\"752\" height=\"768\"&gt;\r\n\r\n  &lt;figcaption class=\"attachment__caption\"&gt;\r\n      Coworking.\r\n  &lt;/figcaption&gt;\r\n&lt;/figure&gt;\r\n&lt;figcaption class=\"attachment__caption attachment__caption--edited\"&gt;Coworking.&lt;/figcaption&gt;&lt;/figure&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;figure data-trix-attachment=\"{&amp;quot;content&amp;quot;:&amp;quot;&lt;figure class=\\&amp;quot;attachment attachment--preview attachment--jpg\\&amp;quot;&gt;\\n    &lt;img src=\\&amp;quot;http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2F-5b3d481c22cafd1ff8db36c1b24e57d8788d2217/article-1.jpg\\&amp;quot; /&gt;\\n\\n  &lt;figcaption class=\\&amp;quot;attachment__caption\\&amp;quot;&gt;\\n      Working, working, working.\\n  &lt;/figcaption&gt;\\n&lt;/figure&gt;\\n&amp;quot;,&amp;quot;contentType&amp;quot;:&amp;quot;image/jpeg&amp;quot;,&amp;quot;filename&amp;quot;:&amp;quot;article-1.jpg&amp;quot;,&amp;quot;filesize&amp;quot;:83730,&amp;quot;height&amp;quot;:800,&amp;quot;previewable&amp;quot;:true,&amp;quot;sgid&amp;quot;:&amp;quot;BAh7CEkiCGdpZAY6BkVU--d99eadb00dd5ae6bb4220f5781b2946c2171604f&amp;quot;,&amp;quot;url&amp;quot;:&amp;quot;http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--5c31ee2db4a2660d4a674dfb54375fda30251d3d/article-1.jpg&amp;quot;,&amp;quot;width&amp;quot;:1200}\" data-trix-content-type=\"image/jpeg\" data-trix-attributes=\"{&amp;quot;caption&amp;quot;:&amp;quot;Working, working, working.&amp;quot;,&amp;quot;presentation&amp;quot;:&amp;quot;gallery&amp;quot;}\" class=\"attachment attachment--content attachment--jpg\"&gt;&lt;figure class=\"attachment attachment--preview attachment--jpg\"&gt;\r\n    &lt;img src=\"http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9i6IkJBaDdCam9VY21WemFYcGxYM1J2WDJ4cGJXbDBXd2RwQWdBRWFRSUFBdz09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--5b3d481c22cafd1ff8db36c1b24e57d8788d2217/article-1.jpg\"&gt;\r\n\r\n  &lt;figcaption class=\"attachment__caption\"&gt;\r\n      Working, working, working.\r\n  &lt;/figcaption&gt;\r\n&lt;/figure&gt;\r\n&lt;figcaption class=\"attachment__caption attachment__caption--edited\"&gt;Working, working, working.&lt;/figcaption&gt;&lt;/figure&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/div&gt;", "user_id"=&gt;"1", "category_list"=&gt;["", "Business", "Real Estate"]}, "commit"=&gt;"Update Article", "id"=&gt;"welcome-to-ftja"}
    Article Load (2.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."slug" = $1 LIMIT $2  [["slug", "welcome-to-ftja"], ["LIMIT", 1]]
    ↳ app/controllers/articles_controller.rb:73:in `set_article'
     (2.1ms)  BEGIN
    ↳ app/controllers/articles_controller.rb:50:in `block in update'
    ActionText::RichText Load (4.0ms)  SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = $1 AND "action_text_rich_texts"."record_type" = $2 AND "action_text_rich_texts"."name" = $3 LIMIT $4  [["record_id", 1], ["record_type", "Article"], ["name", "body"], ["LIMIT", 1]]
    ↳ app/controllers/articles_controller.rb:50:in `block in update'
     (1.3ms)  ROLLBACK
    ↳ app/controllers/articles_controller.rb:50:in `block in update'
  Completed 500 Internal Server Error in 27ms (ActiveRecord: 9.4ms | Allocations: 5092)
  
  
  
  JSON::ParserError (783: unexpected token at '#&lt;ImageUploader::UploadedFile:0x00007fb154adb508&gt;'):
  
  app/controllers/articles_controller.rb:50:in `block in update'
  app/controllers/articles_controller.rb:49:in `update'
  </code></pre>
  
  <p>If you notice the <code>category_list</code> in the params, I believe that may be causing it:</p>
  
  <pre><code>"category_list"=&gt;["", "Business", "Real Estate"]},
  </code></pre>
  
  <p>Based on some Googling I have done, it could be the empty string <code>""</code> that is throwing some JSON Parsing for a loop.</p>
  
  <p>One suggestion I found was to change the Cookie Serializer to <code>:hybrid</code>, like so:</p>
  
  <pre><code>Rails.application.config.action_dispatch.cookies_serializer = :hybrid
  </code></pre>
  
  <p>I tried that and restarted the server and no luck.</p>
  
  <p>What could be causing this and how can I fix it?</p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>At the console on the error page, I tried the following as well:</p>
  
  <pre><code>&gt;&gt; article_params[:category_list]
  =&gt; ["", "Business", "Real Estate"]
  &gt;&gt; article_params[:category_list].to_json
  =&gt; "[\"\",\"Business\",\"Real Estate\"]"
  &gt;&gt; @article.image
  =&gt; #&lt;ImageUploader::UploadedFile storage=:store id="3b96dc3198098a9f57ff2b00a2cdb252.jpg" metadata={"filename"=&gt;"article-3.jpg", "size"=&gt;99083, "mime_type"=&gt;"image/jpeg"}&gt;
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>I am using <code>SimpleForm</code> to handle my forms and the truncated version of this particular form looks like this:</p>
  
  <pre><code>&lt;%= simple_form_for article, html: {multipart: true} do |f| %&gt;
     &lt;%= f.input_field :title, placeholder: "Title", class: "form-control" %&gt;
     &lt;%= f.rich_text_area :body, class: 'form-control customized-min-height', placeholder: "Enter your article body here..." %&gt;
     &lt;%= f.input :user_id, as: :hidden, input_html: { value: current_user.id } %&gt;
  
     &lt;%= f.input_field :category_list, collection: ActsAsTaggableOn::Tag.all.pluck(:name), input_html: { value: article.category_list.join(","), class: "custom-control-input" }, as: :check_boxes %&gt;
  
        &lt;%= f.button :submit, class:"btn btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The above form only produces the <code>""</code> when I go to <code>Articles#Edit</code></p>
  
  <p>This is my <code>Articles#Edit</code> controller action:</p>
  
  <pre><code>  # GET /articles/1/edit
    def edit
    end
  </code></pre>
  
  <p>How do I fix this?</p>
  
  <p><strong>Edit 3</strong></p>
  
  <p>Ok I eventually got the form to not return that empty string by adding <code>include_blank: false</code> and <code>include_hidden: false</code> like so:</p>
  
  <pre><code>&lt;%= f.input_field :category_list, collection: ActsAsTaggableOn::Tag.all.pluck(:name), input_html: { value: article.category_list.join(","), class: "custom-control-input" }, as: :check_boxes, include_blank: false, include_hidden: false %&gt;
  </code></pre>
  
  <p>Which generates params that look like this:</p>
  
  <pre><code>&gt;&gt; article_params
  =&gt; &lt;ActionController::Parameters {"title"=&gt;"Welcome!!", "body"=&gt;"&lt;h1&gt;Yet another great article.&lt;/h1&gt;&lt;div&gt;Test &lt;strong&gt;bold&lt;/strong&gt; &lt;em&gt;italic&lt;/em&gt; &lt;del&gt;strikethrough&lt;br&gt;&lt;br&gt;&lt;/del&gt;&lt;figure data-trix-attachment=\"{&amp;quot;content&amp;quot;:&amp;quot;&lt;figure class=\\&amp;quot;attachment attachment--preview attachment--jpg\\&amp;quot;&gt;\\n    &lt;img src=\\&amp;quot;http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBd2217/cowork-4.jpg\\&amp;quot; /&gt;\\n\\n  &lt;figcaption class=\\&amp;quot;attachment__caption\\&amp;quot;&gt;\\n      Coworking.\\n  &lt;/figcaption&gt;\\n&lt;/figure&gt;\\n&amp;quot;,&amp;quot;contentType&amp;quot;:&amp;quot;image/jpeg&amp;quot;,&amp;quot;filename&amp;quot;:&amp;quot;cowork-4.jpg&amp;quot;,&amp;quot;filesize&amp;quot;:82497,&amp;quot;height&amp;quot;:768,&amp;quot;previewable&amp;quot;:true,&amp;quot;sgid&amp;quot;:&amp;quot;BAhaac&amp;quot;,&amp;quot;url&amp;quot;:&amp;quot;http://localhost:3000/rails/active_storage/blobs/eya9338/cowork-4.jpg&amp;quot;,&amp;quot;width&amp;quot;:752}\" data-trix-content-type=\"image/jpeg\" data-trix-attributes=\"{&amp;quot;caption&amp;quot;:&amp;quot;Coworking.&amp;quot;,&amp;quot;presentation&amp;quot;:&amp;quot;gallery&amp;quot;}\" class=\"attachment attachment--content attachment--jpg\"&gt;&lt;figure class=\"attachment attachment--preview attachment--jpg\"&gt;\r\n    &lt;img src=\"http://localhost:3000/rails/active_storage/representations/e88d2217/cowork-4.jpg\"&gt;\r\n\r\n  &lt;figcaption class=\"attachment__caption\"&gt;\r\n      Coworking.\r\n  &lt;/figcaption&gt;\r\n&lt;/figure&gt;\r\n&lt;figcaption class=\"attachment__caption attachment__caption--edited\"&gt;Coworking.&lt;/figcaption&gt;&lt;/figure&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;figure data-trix-attachment=\"{&amp;quot;content&amp;quot;:&amp;quot;&lt;figure class=\\&amp;quot;attachment attachment--preview attachment--jpg\\&amp;quot;&gt;\\n    &lt;img src=\\&amp;quot;http://localhost:3000/rails/active_storage/representations/eyJ8d2217/article-1.jpg\\&amp;quot; /&gt;\\n\\n  &lt;figcaption class=\\&amp;quot;attachment__caption\\&amp;quot;&gt;\\n      Working, working, working.\\n  &lt;/figcaption&gt;\\n&lt;/figure&gt;\\n&amp;quot;,&amp;quot;contentType&amp;quot;:&amp;quot;image/jpeg&amp;quot;,&amp;quot;filename&amp;quot;:&amp;quot;article-1.jpg&amp;quot;,&amp;quot;filesize&amp;quot;:83730,&amp;quot;height&amp;quot;:800,&amp;quot;previewable&amp;quot;:true,&amp;quot;sgid&amp;quot;:&amp;quot;B1604f&amp;quot;,&amp;quot;url&amp;quot;:&amp;quot;http://localhost:3000/rails/active_storage/blobs/eyd3d/article-1.jpg&amp;quot;,&amp;quot;width&amp;quot;:1200}\" data-trix-content-type=\"image/jpeg\" data-trix-attributes=\"{&amp;quot;caption&amp;quot;:&amp;quot;Working, working, working.&amp;quot;,&amp;quot;presentation&amp;quot;:&amp;quot;gallery&amp;quot;}\" class=\"attachment attachment--content attachment--jpg\"&gt;&lt;figure class=\"attachment attachment--preview attachment--jpg\"&gt;\r\n    &lt;img src=\"http://localhost:3000/rails/active_storage/blobs/eyd3d/article-1.jpg\" width=\"1200\" height=\"800\"&gt;\r\n\r\n  &lt;figcaption class=\"attachment__caption\"&gt;\r\n      Working, working, working.\r\n  &lt;/figcaption&gt;\r\n&lt;/figure&gt;\r\n&lt;figcaption class=\"attachment__caption attachment__caption--edited\"&gt;Working, working, working.&lt;/figcaption&gt;&lt;/figure&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/div&gt;", "featured"=&gt;"true", "user_id"=&gt;"1", "image"=&gt;"#&lt;ImageUploader::UploadedFile:0x00007fd18d61b1e0&gt;", "category_list"=&gt;["Business", "Real Estate", "Investing"]} permitted: true&gt;
  </code></pre>
  
  <p>But I am still getting the same error. So it seems there is another culprit.</p>