首页 > 解决方案 > nil 的未定义方法“匹配”:NilClass 在保存带有载波附件的对象时

问题描述

控制器似乎正在平稳地移动:

 Pict Create (0.8ms)  INSERT INTO "picts" ("usercontent_id", "image", "caption", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["usercontent_id", 94], ["image", "croc1.jpg"], ["caption", "emotional rescue"], ["user_id", 11], ["created_at", "2019-05-13 20:11:03.353852"], ["updated_at", "2019-05-13 20:11:03.353852"]]
  ↳ app/controllers/picts_controller.rb:36
   (0.6ms)  ROLLBACK

但回滚save命令(下面的第 3 行)

@pict = Pict.new(pict_params)
authorize @pict
@pict.save

我删除了所有验证(尽管在 rails 5.2 中,belongs_to 被强制执行——我仍然通过设置进行了测试optional: true),包括一个文件大小验证器......结果总是一样的。

坦率地说,我什至无法想象 nil 除了适用于什么之外@pict,但它的所有属性都有适当的参数,并通过 Pundit 策略进行访问控制。

因此,我假设载波引擎有问题。carrierwave-aws也安装和相同

CarrierWave.configure do |config|

设置用作现有的功能应用程序。该模型声明

mount_uploader :image, ImageUploader 

谁反过来:

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :aws

参数确实显示了一个图像文件被放置在一个临时 bin 中。

{"utf8"=>"✓",
 "authenticity_token"=>"/ZY1FmOBBlVUdfayK3w7ws+4JgrbLt7U9gjvdyniOIqPMfvB10TUYEgvlDyyTTN9SCOu2zbYdb39Ydd37XdXvg==",
 "pict"=>
  {"nazione_id"=>"15",
   "image"=>
    #<ActionDispatch::Http::UploadedFile:0x00007f72d76516b0
     @content_type="image/jpeg",
     @headers="Content-Disposition: form-data; name=\"pict[image]\"; filename=\"croc1.jpg\"\r\n" + "Content-Type: image/jpeg\r\n",
     @original_filename="croc1.jpg",
     @tempfile=#<File:/tmp/RackMultipart20190513-9509-f934ua.jpg>>,
   "caption"=>"emotional rescue",
   "regione_id"=>""},
 "usercontent_point"=>"Mitchell street darwin",
 "commit"=>" Add ",
 "locale"=>"en"}

是什么导致了undefined method 'match' for nil:NilClass错误?

更新

aws-partitions (1.151.0) lib/aws-partitions/endpoint_provider.rb:82:in `block in partition_matching_region'
aws-partitions (1.151.0) lib/aws-partitions/endpoint_provider.rb:81:in `each'
aws-partitions (1.151.0) lib/aws-partitions/endpoint_provider.rb:81:in `find'
aws-partitions (1.151.0) lib/aws-partitions/endpoint_provider.rb:81:in `partition_matching_region'
aws-partitions (1.151.0) lib/aws-partitions/endpoint_provider.rb:70:in `get_partition'
aws-partitions (1.151.0) lib/aws-partitions/endpoint_provider.rb:23:in `signing_region'
aws-partitions (1.151.0) lib/aws-partitions/endpoint_provider.rb:99:in `signing_region'
aws-sdk-s3 (1.36.1) lib/aws-sdk-s3/plugins/s3_signer.rb:20:in `block in <class:S3Signer>'
aws-sdk-core (3.48.4) lib/seahorse/client/configuration.rb:70:in `call'
aws-sdk-core (3.48.4) lib/seahorse/client/configuration.rb:213:in `block in resolve_defaults'
aws-sdk-core (3.48.4) lib/seahorse/client/configuration.rb:57:in `each'
aws-sdk-core (3.48.4) lib/seahorse/client/configuration.rb:57:in `each'
aws-sdk-core (3.48.4) lib/seahorse/client/configuration.rb:212:in `resolve_defaults'
aws-sdk-core (3.48.4) lib/seahorse/client/configuration.rb:205:in `value_at'
aws-sdk-core (3.48.4) lib/seahorse/client/configuration.rb:189:in `block in resolve'

标签: ruby-on-railscarrierwave

解决方案


undefined method 'match' for nil:NilClass 表示该方法试图在空的东西上运行。

正如一条评论所建议的,这可能是一个糟糕的配置。

另一个假设是没有什么可以匹配的。缺少整个配置块。该文件可能位于错误的目录中或不存在。

这种特殊情况对deploy.rb开发服务器的配置错误,没有指向材料文件的符号链接


推荐阅读