NoMethodError: undefined 方法 'body' for nil:NilClass



我正在尝试重命名上传的文件,这是我在 image_uploader.rb 上的代码:

def filename
if !cached? && file.present? 
new_filename = 'wow.jpg'
new_path = File.join(File.dirname(file.path), new_filename)
new_sf = CarrierWave::SanitizedFile.new(new_path)
cache!(new_sf)
recreate_versions!
new_filename
else 
super 
end 
end

边做边做

recreate_versions!

我遇到了这个错误:

NoMethodError: undefined 方法 'body' for nil:NilClass

如果您尝试重命名已上传和存储的文件,则可以使用move_to方法(文档(。

假设您有一个User模型,其中安装了用于avatar的载波。然后,从 rails 控制台,您可以调用:

User.find(1).avatar.path
=> /path/to/avatar/1/image.jpg
User.find(1).avatar.file.move_to("/path/to/avatar/1/new_image_name.jpg")
User.find(1).avatar.path
=> /path/to/avatar/1/new_image_name.jpg

不需要自定义代码,您的问题应该得到解决。

相关内容

最新更新