JSON 解析器错误 783:params 的意外标记

JSON Parser Error 783: unexpected token at for params

我正在编辑资源 - @article - 每当我提交它时,我都会收到以下错误:

JSON::ParserError in ArticlesController#update
783: unexpected token at '#<ImageUploader::UploadedFile:0x00007fb154adb508>'

它突出显示了这一行:

  if @article.update(article_params)

我的 Article#Update 操作中的正常 Rails 样板代码是什么:

  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

这是服务器日志的完整输出(包括参数):

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"=>"0pO56DaVEiWLX3iE3HNHRKsAPOK2z4RmUaH3jal5rOZjk7zYeoaSru9xOE9GYzLckTbnrVrz4ihXrc1hWcXzXA==", "article"=>{"title"=>"Welcome to FTJA!!", "featured"=>"true", "image"=>"#<ImageUploader::UploadedFile:0x00007fb154adb508>", "body"=>"<h1>Yet another great article.</h1><div>Test <strong>bold</strong> <em>italic</em> <del>strikethrough<br><br></del><figure data-trix-attachment=\"{&quot;content&quot;:&quot;<figure class=\&quot;attachment attachment--preview attachment--jpg\&quot;>\n    <img src=\&quot;http://localhost:3000/rails/active_storage/representations/eyJfcJ2YXJpYXRpb24ifX0=--5b3d481c22cafd1ff8db36c1b24e57d8788d2217/cowork-4.jpg\&quot; />\n\n  <figcaption class=\&quot;attachment__caption\&quot;>\n      Coworking.\n  </figcaption>\n</figure>\n&quot;,&quot;contentType&quot;:&quot;image/jpeg&quot;,&quot;filename&quot;:&quot;cowork-4.jpg&quot;,&quot;filesize&quot;:82497,&quot;height&quot;:768,&quot;previewable&quot;:true,&quot;sgid&quot;:&quot;_ZXhwaXJlc19pbgY7AFRJIgxwdXJwb3NlBjsAVEkiD2F0dGFjaGFibGUGOwBUSSIPZXhwaXJlc19hdAY7AFQw--e56d2e422&quot;,&quot;url&quot;:&quot;http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6d89ec207ef10c8e48577a5e9a170a8d178a9338/cowork-4.jpg&quot;,&quot;width&quot;:752}\" data-trix-content-type=\"image/jpeg\" data-trix-attributes=\"{&quot;caption&quot;:&quot;Coworking.&quot;,&quot;presentation&quot;:&quot;gallery&quot;}\" class=\"attachment attachment--content attachment--jpg\"><figure class=\"attachment attachment--preview attachment--jpg\">\r\n    <img src=\"http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6d89ec207ef10c8e48577a5e9a170a8d178a9338/cowork-4.jpg\" width=\"752\" height=\"768\">\r\n\r\n  <figcaption class=\"attachment__caption\">\r\n      Coworking.\r\n  </figcaption>\r\n</figure>\r\n<figcaption class=\"attachment__caption attachment__caption--edited\">Coworking.</figcaption></figure><br><br><br><figure data-trix-attachment=\"{&quot;content&quot;:&quot;<figure class=\&quot;attachment attachment--preview attachment--jpg\&quot;>\n    <img src=\&quot;http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2F-5b3d481c22cafd1ff8db36c1b24e57d8788d2217/article-1.jpg\&quot; />\n\n  <figcaption class=\&quot;attachment__caption\&quot;>\n      Working, working, working.\n  </figcaption>\n</figure>\n&quot;,&quot;contentType&quot;:&quot;image/jpeg&quot;,&quot;filename&quot;:&quot;article-1.jpg&quot;,&quot;filesize&quot;:83730,&quot;height&quot;:800,&quot;previewable&quot;:true,&quot;sgid&quot;:&quot;BAh7CEkiCGdpZAY6BkVU--d99eadb00dd5ae6bb4220f5781b2946c2171604f&quot;,&quot;url&quot;:&quot;http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--5c31ee2db4a2660d4a674dfb54375fda30251d3d/article-1.jpg&quot;,&quot;width&quot;:1200}\" data-trix-content-type=\"image/jpeg\" data-trix-attributes=\"{&quot;caption&quot;:&quot;Working, working, working.&quot;,&quot;presentation&quot;:&quot;gallery&quot;}\" class=\"attachment attachment--content attachment--jpg\"><figure class=\"attachment attachment--preview attachment--jpg\">\r\n    <img src=\"http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9i6IkJBaDdCam9VY21WemFYcGxYM1J2WDJ4cGJXbDBXd2RwQWdBRWFRSUFBdz09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--5b3d481c22cafd1ff8db36c1b24e57d8788d2217/article-1.jpg\">\r\n\r\n  <figcaption class=\"attachment__caption\">\r\n      Working, working, working.\r\n  </figcaption>\r\n</figure>\r\n<figcaption class=\"attachment__caption attachment__caption--edited\">Working, working, working.</figcaption></figure><br><br><br></div>", "user_id"=>"1", "category_list"=>["", "Business", "Real Estate"]}, "commit"=>"Update Article", "id"=>"welcome-to-ftja"}
  Article Load (2.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."slug" =  LIMIT   [["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" =  AND "action_text_rich_texts"."record_type" =  AND "action_text_rich_texts"."name" =  LIMIT   [["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 '#<ImageUploader::UploadedFile:0x00007fb154adb508>'):

app/controllers/articles_controller.rb:50:in `block in update'
app/controllers/articles_controller.rb:49:in `update'

如果您注意到参数中的 category_list,我相信这可能是造成它的原因:

"category_list"=>["", "Business", "Real Estate"]},

根据我所做的一些谷歌搜索,可能是空字符串 "" 引发了一些 JSON 循环解析。

我发现的一个建议是将 Cookie Serializer 更改为 :hybrid,如下所示:

Rails.application.config.action_dispatch.cookies_serializer = :hybrid

我试过了并重新启动了服务器,但没有成功。

可能是什么原因造成的,我该如何解决?

编辑 1

在错误页面的控制台上,我也尝试了以下操作:

>> article_params[:category_list]
=> ["", "Business", "Real Estate"]
>> article_params[:category_list].to_json
=> "[\"\",\"Business\",\"Real Estate\"]"
>> @article.image
=> #<ImageUploader::UploadedFile storage=:store id="3b96dc3198098a9f57ff2b00a2cdb252.jpg" metadata={"filename"=>"article-3.jpg", "size"=>99083, "mime_type"=>"image/jpeg"}>

编辑 2

我正在使用 SimpleForm 来处理我的表单,这个特定表单的截断版本如下所示:

<%= simple_form_for article, html: {multipart: true} do |f| %>
   <%= f.input_field :title, placeholder: "Title", class: "form-control" %>
   <%= f.rich_text_area :body, class: 'form-control customized-min-height', placeholder: "Enter your article body here..." %>
   <%= f.input :user_id, as: :hidden, input_html: { value: current_user.id } %>

   <%= 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 %>

      <%= f.button :submit, class:"btn btn-primary" %>
<% end %>

当我转到 Articles#Edit

时,上面的表格只生成 ""

这是我的 Articles#Edit 控制器操作:

  # GET /articles/1/edit
  def edit
  end

我该如何解决这个问题?

编辑 3

好的,我最终通过添加 include_blank: falseinclude_hidden: false 得到了不 return 空字符串的表格,如下所示:

<%= 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 %>

生成如下所示的参数:

>> article_params
=> <ActionController::Parameters {"title"=>"Welcome!!", "body"=>"<h1>Yet another great article.</h1><div>Test <strong>bold</strong> <em>italic</em> <del>strikethrough<br><br></del><figure data-trix-attachment=\"{&quot;content&quot;:&quot;<figure class=\&quot;attachment attachment--preview attachment--jpg\&quot;>\n    <img src=\&quot;http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBd2217/cowork-4.jpg\&quot; />\n\n  <figcaption class=\&quot;attachment__caption\&quot;>\n      Coworking.\n  </figcaption>\n</figure>\n&quot;,&quot;contentType&quot;:&quot;image/jpeg&quot;,&quot;filename&quot;:&quot;cowork-4.jpg&quot;,&quot;filesize&quot;:82497,&quot;height&quot;:768,&quot;previewable&quot;:true,&quot;sgid&quot;:&quot;BAhaac&quot;,&quot;url&quot;:&quot;http://localhost:3000/rails/active_storage/blobs/eya9338/cowork-4.jpg&quot;,&quot;width&quot;:752}\" data-trix-content-type=\"image/jpeg\" data-trix-attributes=\"{&quot;caption&quot;:&quot;Coworking.&quot;,&quot;presentation&quot;:&quot;gallery&quot;}\" class=\"attachment attachment--content attachment--jpg\"><figure class=\"attachment attachment--preview attachment--jpg\">\r\n    <img src=\"http://localhost:3000/rails/active_storage/representations/e88d2217/cowork-4.jpg\">\r\n\r\n  <figcaption class=\"attachment__caption\">\r\n      Coworking.\r\n  </figcaption>\r\n</figure>\r\n<figcaption class=\"attachment__caption attachment__caption--edited\">Coworking.</figcaption></figure><br><br><br><figure data-trix-attachment=\"{&quot;content&quot;:&quot;<figure class=\&quot;attachment attachment--preview attachment--jpg\&quot;>\n    <img src=\&quot;http://localhost:3000/rails/active_storage/representations/eyJ8d2217/article-1.jpg\&quot; />\n\n  <figcaption class=\&quot;attachment__caption\&quot;>\n      Working, working, working.\n  </figcaption>\n</figure>\n&quot;,&quot;contentType&quot;:&quot;image/jpeg&quot;,&quot;filename&quot;:&quot;article-1.jpg&quot;,&quot;filesize&quot;:83730,&quot;height&quot;:800,&quot;previewable&quot;:true,&quot;sgid&quot;:&quot;B1604f&quot;,&quot;url&quot;:&quot;http://localhost:3000/rails/active_storage/blobs/eyd3d/article-1.jpg&quot;,&quot;width&quot;:1200}\" data-trix-content-type=\"image/jpeg\" data-trix-attributes=\"{&quot;caption&quot;:&quot;Working, working, working.&quot;,&quot;presentation&quot;:&quot;gallery&quot;}\" class=\"attachment attachment--content attachment--jpg\"><figure class=\"attachment attachment--preview attachment--jpg\">\r\n    <img src=\"http://localhost:3000/rails/active_storage/blobs/eyd3d/article-1.jpg\" width=\"1200\" height=\"800\">\r\n\r\n  <figcaption class=\"attachment__caption\">\r\n      Working, working, working.\r\n  </figcaption>\r\n</figure>\r\n<figcaption class=\"attachment__caption attachment__caption--edited\">Working, working, working.</figcaption></figure><br><br><br></div>", "featured"=>"true", "user_id"=>"1", "image"=>"#<ImageUploader::UploadedFile:0x00007fd18d61b1e0>", "category_list"=>["Business", "Real Estate", "Investing"]} permitted: true>

但我仍然遇到同样的错误。所以似乎还有另一个罪魁祸首。

查看错误信息

783: unexpected token at '#<ImageUploader::UploadedFile:0x00007fb154adb508>'

它似乎永远不会到达空字符串(即使那样也可能会给您带来错误)。解析 ImageUploader 参数时遇到问题。

从其他人那里读到这个错误似乎这可能与插件有关?并且此数据的解析和转换失败。

据我了解,您在基于 simple_form 的表单的富文本字段中添加了大约一张图片。当它试图将图像转换为 json 时失败了。

所以我建议您看看 gem 您是否有关于图片上传的信息?我无法在 Simple_form github 上找到任何类似的问题,所以我认为上传者部分不在 simple_form 本身。但是通过问题从 Simple_form 开发人员那里获得帮助可能是解决这个问题的好方法。

经过大量研究和无数次试错之后,我偶然发现了修复方法。

基本上,似乎正在发生的事情是,只要表单当前有图像并且正在编辑,如果现有图像处理不当,它就会将 cached_image 数据转换为字符串。可以看到一个better explanation here.

罪魁祸首原来是我表格中的这一行:

<%= f.input :image, as: :hidden, value: f.object.cached_image_data %>

一旦我将其更改为以下内容,它就像一个魅力:

<%= f.input :image, as: :hidden, input_html: { value: f.object.cached_image_data } %>